from os.path import dirname, join
import re

import numpy as np
from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal
import pandas as pd
import pytest

from sksurv.datasets import load_bmt, load_cgvhd
from sksurv.nonparametric import (
    CensoringDistributionEstimator,
    SurvivalFunctionEstimator,
    cumulative_incidence_competing_risks,
    kaplan_meier_estimator,
    nelson_aalen_estimator,
)
from sksurv.testing import FixtureParameterFactory
from sksurv.util import Surv

CHANNING_FILE = join(dirname(__file__), "data", "channing.csv")
AIDS_CHILDREN_FILE = join(dirname(__file__), "data", "Lagakos_AIDS_children.csv")
AIDS_ADULTS_FILE = join(dirname(__file__), "data", "Lagakos_AIDS_adults.csv")
CGVHD_AALEN_FILE = join(dirname(__file__), "data", "cgvhd_aalen.npy")
CGVHD_DELTA_FILE = join(dirname(__file__), "data", "cgvhd_delta.npy")
CGVHD_DINSE_FILE = join(dirname(__file__), "data", "cgvhd_dinse.npy")


class SimpleDataKMCases(FixtureParameterFactory):
    @property
    def time(self):
        return [1, 2, 2, 3, 7, 6, 5, 5, 3, 9, 11, 13, 17, 13, 6, 23]

    @property
    def true_x(self):
        return np.array([1, 2, 3, 5, 6, 7, 9, 11, 13, 17, 23])

    def data_all_uncensored(self):
        time = self.time
        event = np.repeat(True, len(time))
        true_y = np.array([0.9375, 0.8125, 0.6875, 0.5625, 0.4375, 0.375, 0.3125, 0.25, 0.125, 0.0625, 0])
        km_ci = np.array(
            [
                [
                    0.632345441738904,
                    0.524604310826649,
                    0.404603278696107,
                    0.295426730710439,
                    0.198117175236982,
                    0.154226191252824,
                    0.113871208031829,
                    0.0775371047405283,
                    0.0206750254850577,
                    0.00409816532132047,
                    0,
                ],
                [
                    0.990953141744666,
                    0.935352257965829,
                    0.856279177922122,
                    0.76223865703927,
                    0.6556436847056,
                    0.597718461149743,
                    0.536497040068499,
                    0.471616563850962,
                    0.327984354552463,
                    0.246994462218279,
                    0,
                ],
            ]
        )
        return time, event, self.true_x, true_y, km_ci

    def data_all_censored(self):
        time = self.time
        event = np.repeat(False, len(time))
        true_x = self.true_x
        true_y = np.ones(true_x.shape[0])
        km_var = np.ones((2, true_x.shape[0]))
        return time, event, true_x, true_y, km_var

    def data_first_censored(self):
        time = self.time
        event = np.repeat(True, len(time))
        event[0] = False
        true_y = np.array(
            [
                1,
                0.866666666666667,
                0.733333333333333,
                0.6,
                0.466666666666667,
                0.4,
                0.333333333333333,
                0.266666666666667,
                0.133333333333333,
                0.0666666666666667,
                0,
            ]
        )
        km_var = np.array(
            [
                [
                    1.0,
                    0.563912005708952,
                    0.436208202355272,
                    0.317586910207385,
                    0.212259381879575,
                    0.164939278037332,
                    0.121545719743796,
                    0.0825808249254053,
                    0.0218732094519415,
                    0.00430036344937564,
                    0,
                ],
                [
                    1.0,
                    0.964884495916472,
                    0.89051993143724,
                    0.796522795577473,
                    0.687454424870113,
                    0.627586046596913,
                    0.563999542746448,
                    0.49633566553142,
                    0.345732210952466,
                    0.260321963072145,
                    0,
                ],
            ]
        )

        return time, event, self.true_x, true_y, km_var

    def data_last_censored(self):
        time = self.time
        event = np.repeat(True, len(time))
        event[-1] = False
        true_y = np.array([0.9375, 0.8125, 0.6875, 0.5625, 0.4375, 0.375, 0.3125, 0.25, 0.125, 0.0625, 0.0625])
        km_var = np.array(
            [
                [
                    0.632345441738904,
                    0.524604310826649,
                    0.404603278696107,
                    0.295426730710439,
                    0.198117175236982,
                    0.154226191252824,
                    0.113871208031829,
                    0.0775371047405283,
                    0.0206750254850577,
                    0.00409816532132047,
                    0.00409816532132047,
                ],
                [
                    0.990953141744666,
                    0.935352257965829,
                    0.856279177922122,
                    0.76223865703927,
                    0.6556436847056,
                    0.597718461149743,
                    0.536497040068499,
                    0.471616563850962,
                    0.327984354552463,
                    0.246994462218279,
                    0.246994462218279,
                ],
            ]
        )
        return time, event, self.true_x, true_y, km_var

    def data_first_and_last_censored(self):
        time = self.time
        event = np.repeat(True, len(time))
        event[0] = False
        event[-1] = False
        true_y = np.array(
            [
                1,
                0.866666666666667,
                0.733333333333333,
                0.6,
                0.466666666666667,
                0.4,
                0.333333333333333,
                0.266666666666667,
                0.133333333333333,
                0.0666666666666667,
                0.0666666666666667,
            ]
        )
        km_var = np.array(
            [
                [
                    1.0,
                    0.563912005708952,
                    0.436208202355272,
                    0.317586910207385,
                    0.212259381879575,
                    0.164939278037332,
                    0.121545719743796,
                    0.0825808249254053,
                    0.0218732094519415,
                    0.00430036344937564,
                    0.00430036344937564,
                ],
                [
                    1.0,
                    0.964884495916472,
                    0.89051993143724,
                    0.796522795577473,
                    0.687454424870113,
                    0.627586046596913,
                    0.563999542746448,
                    0.49633566553142,
                    0.345732210952466,
                    0.260321963072145,
                    0.260321963072145,
                ],
            ]
        )
        return time, event, self.true_x, true_y, km_var


class SimpleDataNACases(FixtureParameterFactory):
    @property
    def time(self):
        return [1, 2, 2, 3, 7, 6, 5, 5, 3, 9, 11, 13, 17, 13, 6, 23]

    @property
    def true_x(self):
        return np.array([1, 2, 3, 5, 6, 7, 9, 11, 13, 17, 23])

    def data_all_uncensored(self):
        time = self.time
        event = np.repeat(True, len(time))
        true_y = np.array(
            [
                0.0625,
                0.195833333333333,
                0.349679487179487,
                0.531497668997669,
                0.753719891219891,
                0.896577034077034,
                1.0632437007437,
                1.2632437007437,
                1.7632437007437,
                2.2632437007437,
                3.2632437007437,
            ]
        )

        return time, event, self.true_x, true_y

    def data_first_censored(self):
        time = self.time
        event = np.repeat(True, len(time))
        event[0] = False
        true_y = np.array(
            [
                0,
                0.133333333333333,
                0.287179487179487,
                0.468997668997669,
                0.691219891219891,
                0.834077034077034,
                1.0007437007437,
                1.2007437007437,
                1.7007437007437,
                2.2007437007437,
                3.2007437007437,
            ]
        )

        return time, event, self.true_x, true_y

    def data_last_censored(self):
        time = self.time
        event = np.repeat(True, len(time))
        event[-1] = False
        true_y = np.array(
            [
                0.0625,
                0.195833333333333,
                0.349679487179487,
                0.531497668997669,
                0.753719891219891,
                0.896577034077034,
                1.0632437007437,
                1.2632437007437,
                1.7632437007437,
                2.2632437007437,
                2.2632437007437,
            ]
        )

        return time, event, self.true_x, true_y

    def data_first_and_last_censored(self):
        time = self.time
        event = np.repeat(True, len(time))
        event[0] = False
        event[-1] = False
        true_y = np.array(
            [
                0,
                0.133333333333333,
                0.287179487179487,
                0.468997668997669,
                0.691219891219891,
                0.834077034077034,
                1.0007437007437,
                1.2007437007437,
                1.7007437007437,
                2.2007437007437,
                2.2007437007437,
            ]
        )

        return time, event, self.true_x, true_y


class Whas500CIData(FixtureParameterFactory):
    def data_loglog_ci_95(self):
        true_ci = np.array(
            [
                [
                    0.96826186,
                    0.9482958,
                    0.94106981,
                    0.93630832,
                    0.93158523,
                    0.91991943,
                    0.9061371,
                    0.89931822,
                    0.89029004,
                    0.8858006,
                    0.88356159,
                    0.87909448,
                    0.87241954,
                    0.86577333,
                    0.86135743,
                    0.85695281,
                    0.85475457,
                    0.85255896,
                    0.84817541,
                    0.84161846,
                    0.83943752,
                    0.83725886,
                    0.83508242,
                    0.83290818,
                    0.8307361,
                    0.82856612,
                    0.82639822,
                    0.82423236,
                    0.81990662,
                    0.81774667,
                    0.81558863,
                    0.81343247,
                    0.80912565,
                    0.80482599,
                    0.80267878,
                    0.80053328,
                    0.79838947,
                    0.79624733,
                    0.79410682,
                    0.79196793,
                    0.78983064,
                    0.78769492,
                    0.78556076,
                    0.78342813,
                    0.78129702,
                    0.7791674,
                    0.77703926,
                    0.77491258,
                    0.77278735,
                    0.77066353,
                    0.76854113,
                    0.76642012,
                    0.76430048,
                    0.76218221,
                    0.76006528,
                    0.75583541,
                    0.75372243,
                    0.75161075,
                    0.74950034,
                    0.7473912,
                    0.7452833,
                    0.74107122,
                    0.736864,
                    0.73476219,
                    0.73266155,
                    0.73056209,
                    0.72846379,
                    0.72636664,
                    0.72427062,
                    0.72008198,
                    0.71798933,
                    0.71589778,
                    0.71380732,
                    0.71171795,
                    0.70962965,
                    0.70545626,
                    0.70337114,
                    0.70128706,
                    0.69920402,
                    0.69712201,
                    0.69504101,
                    0.69296103,
                    0.69088206,
                    0.68880408,
                    0.6846511,
                    0.68257608,
                    0.68257608,
                    0.68257608,
                    0.68257608,
                    0.68257608,
                    0.68045678,
                    0.67833853,
                    0.67833853,
                    0.67833853,
                    0.67620125,
                    0.67406505,
                    0.67406505,
                    0.67406505,
                    0.67406505,
                    0.67406505,
                    0.67188843,
                    0.66971297,
                    0.66971297,
                    0.66971297,
                    0.66971297,
                    0.66971297,
                    0.66971297,
                    0.66971297,
                    0.66748794,
                    0.66748794,
                    0.66525672,
                    0.66525672,
                    0.66525672,
                    0.66525672,
                    0.66525672,
                    0.66525672,
                    0.66525672,
                    0.66297306,
                    0.66297306,
                    0.66065074,
                    0.66065074,
                    0.66065074,
                    0.66065074,
                    0.66065074,
                    0.66065074,
                    0.66065074,
                    0.66065074,
                    0.65823649,
                    0.65823649,
                    0.65581505,
                    0.6533953,
                    0.6533953,
                    0.6533953,
                    0.65095919,
                    0.65095919,
                    0.65095919,
                    0.64850644,
                    0.64850644,
                    0.64850644,
                    0.64850644,
                    0.64850644,
                    0.64850644,
                    0.64850644,
                    0.64850644,
                    0.64850644,
                    0.64850644,
                    0.64850644,
                    0.64850644,
                    0.64591859,
                    0.64591859,
                    0.64331202,
                    0.64069705,
                    0.63808425,
                    0.63808425,
                    0.63808425,
                    0.63808425,
                    0.63540803,
                    0.63540803,
                    0.63272287,
                    0.63004007,
                    0.63004007,
                    0.63004007,
                    0.63004007,
                    0.63004007,
                    0.63004007,
                    0.63004007,
                    0.63004007,
                    0.63004007,
                    0.6272408,
                    0.62444424,
                    0.62444424,
                    0.62444424,
                    0.62162521,
                    0.6188089,
                    0.6159953,
                    0.61318435,
                    0.61037605,
                    0.61037605,
                    0.61037605,
                    0.6075444,
                    0.60471541,
                    0.60471541,
                    0.60187583,
                    0.59903891,
                    0.59620461,
                    0.59620461,
                    0.59335944,
                    0.59051688,
                    0.58767692,
                    0.58483951,
                    0.58200465,
                    0.57917229,
                    0.57634242,
                    0.57351502,
                    0.57069006,
                    0.56786752,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56504738,
                    0.56196974,
                    0.56196974,
                    0.56196974,
                    0.56196974,
                    0.5588094,
                    0.5588094,
                    0.55563482,
                    0.55563482,
                    0.55563482,
                    0.55563482,
                    0.55563482,
                    0.55239024,
                    0.55239024,
                    0.55239024,
                    0.54911132,
                    0.54911132,
                    0.54911132,
                    0.54911132,
                    0.54911132,
                    0.54911132,
                    0.54911132,
                    0.54911132,
                    0.54911132,
                    0.54563039,
                    0.54563039,
                    0.54563039,
                    0.54563039,
                    0.54208727,
                    0.54208727,
                    0.54208727,
                    0.54208727,
                    0.53847936,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.53482859,
                    0.5306807,
                    0.5306807,
                    0.5306807,
                    0.5306807,
                    0.5306807,
                    0.5306807,
                    0.5306807,
                    0.5306807,
                    0.52627071,
                    0.52627071,
                    0.52627071,
                    0.52627071,
                    0.52627071,
                    0.52627071,
                    0.52627071,
                    0.52627071,
                    0.52627071,
                    0.52627071,
                    0.52627071,
                    0.52627071,
                    0.52130092,
                    0.52130092,
                    0.52130092,
                    0.52130092,
                    0.52130092,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.51102842,
                    0.50468416,
                    0.49837386,
                    0.49209611,
                    0.48584963,
                    0.47963325,
                    0.47344589,
                    0.46728656,
                    0.46115437,
                    0.45504847,
                    0.44896809,
                    0.44291252,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.43688109,
                    0.42854576,
                    0.42854576,
                    0.42854576,
                    0.42854576,
                    0.42854576,
                    0.42854576,
                    0.42854576,
                    0.42854576,
                    0.42854576,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.41864375,
                    0.3450851,
                    0.3450851,
                    0.3450851,
                    0.3450851,
                    0.3450851,
                    0.3450851,
                    0.3450851,
                    0.3450851,
                    0.3450851,
                    0.08979757,
                    0.0116598,
                    0.0,
                ],
                [
                    0.99196624,
                    0.98027286,
                    0.97559252,
                    0.97241258,
                    0.96919211,
                    0.96099266,
                    0.95092943,
                    0.9458237,
                    0.93895115,
                    0.93548982,
                    0.93375334,
                    0.93026935,
                    0.9250173,
                    0.9197362,
                    0.91620041,
                    0.91265323,
                    0.91087555,
                    0.90909521,
                    0.9055268,
                    0.90015569,
                    0.89836059,
                    0.89656319,
                    0.89476355,
                    0.89296171,
                    0.89115771,
                    0.88935158,
                    0.88754337,
                    0.88573312,
                    0.8821066,
                    0.88029041,
                    0.87847231,
                    0.87665232,
                    0.87300683,
                    0.86935416,
                    0.8675252,
                    0.86569453,
                    0.86386217,
                    0.86202814,
                    0.86019247,
                    0.85835518,
                    0.85651629,
                    0.85467583,
                    0.85283381,
                    0.85099026,
                    0.8491452,
                    0.84729863,
                    0.84545059,
                    0.8436011,
                    0.84175016,
                    0.83989779,
                    0.83804402,
                    0.83618886,
                    0.83433233,
                    0.83247443,
                    0.83061519,
                    0.82689274,
                    0.82502956,
                    0.82316508,
                    0.82129934,
                    0.81943233,
                    0.81756408,
                    0.81382388,
                    0.81007883,
                    0.80820451,
                    0.80632902,
                    0.80445237,
                    0.80257455,
                    0.80069559,
                    0.7988155,
                    0.79505195,
                    0.7931685,
                    0.79128396,
                    0.78939834,
                    0.78751163,
                    0.78562385,
                    0.78184512,
                    0.77995419,
                    0.77806221,
                    0.77616921,
                    0.77427518,
                    0.77238014,
                    0.77048409,
                    0.76858704,
                    0.766689,
                    0.76288997,
                    0.760989,
                    0.760989,
                    0.760989,
                    0.760989,
                    0.760989,
                    0.75905303,
                    0.75711604,
                    0.75711604,
                    0.75711604,
                    0.75516296,
                    0.75320884,
                    0.75320884,
                    0.75320884,
                    0.75320884,
                    0.75320884,
                    0.75122263,
                    0.74923533,
                    0.74923533,
                    0.74923533,
                    0.74923533,
                    0.74923533,
                    0.74923533,
                    0.74923533,
                    0.74720917,
                    0.74720917,
                    0.74517631,
                    0.74517631,
                    0.74517631,
                    0.74517631,
                    0.74517631,
                    0.74517631,
                    0.74517631,
                    0.74310255,
                    0.74310255,
                    0.74099808,
                    0.74099808,
                    0.74099808,
                    0.74099808,
                    0.74099808,
                    0.74099808,
                    0.74099808,
                    0.74099808,
                    0.73882421,
                    0.73882421,
                    0.73664242,
                    0.73445913,
                    0.73445913,
                    0.73445913,
                    0.73226128,
                    0.73226128,
                    0.73226128,
                    0.73004862,
                    0.73004862,
                    0.73004862,
                    0.73004862,
                    0.73004862,
                    0.73004862,
                    0.73004862,
                    0.73004862,
                    0.73004862,
                    0.73004862,
                    0.73004862,
                    0.73004862,
                    0.72773618,
                    0.72773618,
                    0.72540711,
                    0.72306869,
                    0.72072841,
                    0.72072841,
                    0.72072841,
                    0.72072841,
                    0.71833975,
                    0.71833975,
                    0.71594113,
                    0.71354053,
                    0.71354053,
                    0.71354053,
                    0.71354053,
                    0.71354053,
                    0.71354053,
                    0.71354053,
                    0.71354053,
                    0.71354053,
                    0.71105436,
                    0.70856593,
                    0.70856593,
                    0.70856593,
                    0.70605767,
                    0.70354713,
                    0.70103434,
                    0.69851933,
                    0.6960021,
                    0.6960021,
                    0.6960021,
                    0.69346434,
                    0.69092435,
                    0.69092435,
                    0.68837277,
                    0.68581896,
                    0.68326294,
                    0.68326294,
                    0.68069513,
                    0.67812512,
                    0.67555291,
                    0.67297853,
                    0.67040201,
                    0.66782334,
                    0.66524256,
                    0.66265967,
                    0.6600747,
                    0.65748765,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65489855,
                    0.65212108,
                    0.65212108,
                    0.65212108,
                    0.65212108,
                    0.64928059,
                    0.64928059,
                    0.64642468,
                    0.64642468,
                    0.64642468,
                    0.64642468,
                    0.64642468,
                    0.64351454,
                    0.64351454,
                    0.64351454,
                    0.64057471,
                    0.64057471,
                    0.64057471,
                    0.64057471,
                    0.64057471,
                    0.64057471,
                    0.64057471,
                    0.64057471,
                    0.64057471,
                    0.63749077,
                    0.63749077,
                    0.63749077,
                    0.63749077,
                    0.63435761,
                    0.63435761,
                    0.63435761,
                    0.63435761,
                    0.63117333,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.6279523,
                    0.62440189,
                    0.62440189,
                    0.62440189,
                    0.62440189,
                    0.62440189,
                    0.62440189,
                    0.62440189,
                    0.62440189,
                    0.6206771,
                    0.6206771,
                    0.6206771,
                    0.6206771,
                    0.6206771,
                    0.6206771,
                    0.6206771,
                    0.6206771,
                    0.6206771,
                    0.6206771,
                    0.6206771,
                    0.6206771,
                    0.61660741,
                    0.61660741,
                    0.61660741,
                    0.61660741,
                    0.61660741,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60821176,
                    0.60333709,
                    0.59843545,
                    0.59350802,
                    0.58855584,
                    0.58357986,
                    0.57858095,
                    0.57355989,
                    0.5685174,
                    0.56345413,
                    0.55837069,
                    0.55326763,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54814545,
                    0.54180033,
                    0.54180033,
                    0.54180033,
                    0.54180033,
                    0.54180033,
                    0.54180033,
                    0.54180033,
                    0.54180033,
                    0.54180033,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.53469408,
                    0.52716703,
                    0.52716703,
                    0.52716703,
                    0.52716703,
                    0.52716703,
                    0.52716703,
                    0.52716703,
                    0.52716703,
                    0.52716703,
                    0.53337398,
                    0.43538693,
                    0.0,
                ],
            ]
        )
        return 0.95, "log-log", true_ci

    def data_loglog_ci_99(self):
        true_ci = np.array(
            [
                [
                    0.96069455,
                    0.9399525,
                    0.93243483,
                    0.92748604,
                    0.92258173,
                    0.91048906,
                    0.89623994,
                    0.88920432,
                    0.87990263,
                    0.8752826,
                    0.87297976,
                    0.86838778,
                    0.86153226,
                    0.85471307,
                    0.85018585,
                    0.84567295,
                    0.84342168,
                    0.84117376,
                    0.83668769,
                    0.82998206,
                    0.82775286,
                    0.82552657,
                    0.82330315,
                    0.82108253,
                    0.81886467,
                    0.81664951,
                    0.81443702,
                    0.81222714,
                    0.80781506,
                    0.80561278,
                    0.80341295,
                    0.80121553,
                    0.79682777,
                    0.79244924,
                    0.79026334,
                    0.78807965,
                    0.78589814,
                    0.78371877,
                    0.78154151,
                    0.77936635,
                    0.77719324,
                    0.77502217,
                    0.7728531,
                    0.77068602,
                    0.76852089,
                    0.7663577,
                    0.76419641,
                    0.76203702,
                    0.75987948,
                    0.75772379,
                    0.75556993,
                    0.75341786,
                    0.75126758,
                    0.74911906,
                    0.74697228,
                    0.74268388,
                    0.74054223,
                    0.73840224,
                    0.73626391,
                    0.73412722,
                    0.73199215,
                    0.72772682,
                    0.72346779,
                    0.72134061,
                    0.71921495,
                    0.71709082,
                    0.7149682,
                    0.71284706,
                    0.71072741,
                    0.7064925,
                    0.70437722,
                    0.70226337,
                    0.70015094,
                    0.69803991,
                    0.69593029,
                    0.6917152,
                    0.6896097,
                    0.68750557,
                    0.68540278,
                    0.68330133,
                    0.68120121,
                    0.6791024,
                    0.6770049,
                    0.67490871,
                    0.67072018,
                    0.66862783,
                    0.66862783,
                    0.66862783,
                    0.66862783,
                    0.66862783,
                    0.6664896,
                    0.66435276,
                    0.66435276,
                    0.66435276,
                    0.66219635,
                    0.66004137,
                    0.66004137,
                    0.66004137,
                    0.66004137,
                    0.66004137,
                    0.65784454,
                    0.65564922,
                    0.65564922,
                    0.65564922,
                    0.65564922,
                    0.65564922,
                    0.65564922,
                    0.65564922,
                    0.65340254,
                    0.65340254,
                    0.65114973,
                    0.65114973,
                    0.65114973,
                    0.65114973,
                    0.65114973,
                    0.65114973,
                    0.65114973,
                    0.64884252,
                    0.64884252,
                    0.64649531,
                    0.64649531,
                    0.64649531,
                    0.64649531,
                    0.64649531,
                    0.64649531,
                    0.64649531,
                    0.64649531,
                    0.64405235,
                    0.64405235,
                    0.64160234,
                    0.6391546,
                    0.6391546,
                    0.6391546,
                    0.63669021,
                    0.63669021,
                    0.63669021,
                    0.63420891,
                    0.63420891,
                    0.63420891,
                    0.63420891,
                    0.63420891,
                    0.63420891,
                    0.63420891,
                    0.63420891,
                    0.63420891,
                    0.63420891,
                    0.63420891,
                    0.63420891,
                    0.63158651,
                    0.63158651,
                    0.6289451,
                    0.6262955,
                    0.62364883,
                    0.62364883,
                    0.62364883,
                    0.62364883,
                    0.62093623,
                    0.62093623,
                    0.61821494,
                    0.61549681,
                    0.61549681,
                    0.61549681,
                    0.61549681,
                    0.61549681,
                    0.61549681,
                    0.61549681,
                    0.61549681,
                    0.61549681,
                    0.61265704,
                    0.60982092,
                    0.60982092,
                    0.60982092,
                    0.60696197,
                    0.60410669,
                    0.60125505,
                    0.598407,
                    0.59556249,
                    0.59556249,
                    0.59556249,
                    0.59269426,
                    0.58982961,
                    0.58982961,
                    0.58695464,
                    0.58408323,
                    0.58121536,
                    0.58121536,
                    0.57833686,
                    0.57546188,
                    0.57259038,
                    0.56972232,
                    0.56685766,
                    0.56399638,
                    0.56113844,
                    0.5582838,
                    0.55543244,
                    0.55258433,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54973943,
                    0.54662584,
                    0.54662584,
                    0.54662584,
                    0.54662584,
                    0.54342646,
                    0.54342646,
                    0.54021321,
                    0.54021321,
                    0.54021321,
                    0.54021321,
                    0.54021321,
                    0.53692756,
                    0.53692756,
                    0.53692756,
                    0.533607,
                    0.533607,
                    0.533607,
                    0.533607,
                    0.533607,
                    0.533607,
                    0.533607,
                    0.533607,
                    0.533607,
                    0.53007513,
                    0.53007513,
                    0.53007513,
                    0.53007513,
                    0.5264792,
                    0.5264792,
                    0.5264792,
                    0.5264792,
                    0.52281653,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.5191103,
                    0.51487966,
                    0.51487966,
                    0.51487966,
                    0.51487966,
                    0.51487966,
                    0.51487966,
                    0.51487966,
                    0.51487966,
                    0.51037292,
                    0.51037292,
                    0.51037292,
                    0.51037292,
                    0.51037292,
                    0.51037292,
                    0.51037292,
                    0.51037292,
                    0.51037292,
                    0.51037292,
                    0.51037292,
                    0.51037292,
                    0.50527132,
                    0.50527132,
                    0.50527132,
                    0.50527132,
                    0.50527132,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.49472548,
                    0.48815787,
                    0.48163641,
                    0.4751592,
                    0.4687245,
                    0.46233071,
                    0.45597637,
                    0.44966012,
                    0.44338073,
                    0.43713705,
                    0.43092802,
                    0.42475265,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.41861004,
                    0.40999634,
                    0.40999634,
                    0.40999634,
                    0.40999634,
                    0.40999634,
                    0.40999634,
                    0.40999634,
                    0.40999634,
                    0.40999634,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.3996945,
                    0.31589426,
                    0.31589426,
                    0.31589426,
                    0.31589426,
                    0.31589426,
                    0.31589426,
                    0.31589426,
                    0.31589426,
                    0.31589426,
                    0.05095117,
                    0.0030451,
                    0.0,
                ],
                [
                    0.9935331,
                    0.98306404,
                    0.97877429,
                    0.97583977,
                    0.9728543,
                    0.96520344,
                    0.9557383,
                    0.95091126,
                    0.94439222,
                    0.94110059,
                    0.93944733,
                    0.93612663,
                    0.93111211,
                    0.92606022,
                    0.9226729,
                    0.91927093,
                    0.91756465,
                    0.91585496,
                    0.91242561,
                    0.90725769,
                    0.90552894,
                    0.90379723,
                    0.90206262,
                    0.90032516,
                    0.89858491,
                    0.89684191,
                    0.89509622,
                    0.89334788,
                    0.88984344,
                    0.88808742,
                    0.88632893,
                    0.884568,
                    0.88103899,
                    0.87750068,
                    0.87572813,
                    0.87395335,
                    0.87217639,
                    0.87039727,
                    0.86861601,
                    0.86683266,
                    0.86504724,
                    0.86325977,
                    0.86147028,
                    0.85967881,
                    0.85788537,
                    0.85608999,
                    0.85429269,
                    0.8524935,
                    0.85069244,
                    0.84888952,
                    0.84708479,
                    0.84527824,
                    0.84346992,
                    0.84165982,
                    0.83984798,
                    0.83621914,
                    0.83440218,
                    0.83258354,
                    0.83076325,
                    0.82894132,
                    0.82711777,
                    0.82346586,
                    0.81980765,
                    0.81797623,
                    0.81614327,
                    0.81430879,
                    0.81247281,
                    0.81063534,
                    0.8087964,
                    0.80511413,
                    0.80327083,
                    0.8014261,
                    0.79957996,
                    0.79773241,
                    0.79588347,
                    0.79218146,
                    0.79032841,
                    0.788474,
                    0.78661826,
                    0.78476119,
                    0.7829028,
                    0.78104309,
                    0.77918209,
                    0.77731979,
                    0.77359136,
                    0.77172523,
                    0.77172523,
                    0.77172523,
                    0.77172523,
                    0.77172523,
                    0.76982544,
                    0.7679243,
                    0.7679243,
                    0.7679243,
                    0.7660075,
                    0.76408934,
                    0.76408934,
                    0.76408934,
                    0.76408934,
                    0.76408934,
                    0.76214028,
                    0.76018979,
                    0.76018979,
                    0.76018979,
                    0.76018979,
                    0.76018979,
                    0.76018979,
                    0.76018979,
                    0.75820197,
                    0.75820197,
                    0.75620739,
                    0.75620739,
                    0.75620739,
                    0.75620739,
                    0.75620739,
                    0.75620739,
                    0.75620739,
                    0.75417354,
                    0.75417354,
                    0.75211012,
                    0.75211012,
                    0.75211012,
                    0.75211012,
                    0.75211012,
                    0.75211012,
                    0.75211012,
                    0.75211012,
                    0.7499805,
                    0.7499805,
                    0.74784288,
                    0.74570335,
                    0.74570335,
                    0.74570335,
                    0.74354953,
                    0.74354953,
                    0.74354953,
                    0.74138119,
                    0.74138119,
                    0.74138119,
                    0.74138119,
                    0.74138119,
                    0.74138119,
                    0.74138119,
                    0.74138119,
                    0.74138119,
                    0.74138119,
                    0.74138119,
                    0.74138119,
                    0.73911808,
                    0.73911808,
                    0.73683869,
                    0.73454987,
                    0.73225867,
                    0.73225867,
                    0.73225867,
                    0.73225867,
                    0.72992126,
                    0.72992126,
                    0.72757382,
                    0.72522385,
                    0.72522385,
                    0.72522385,
                    0.72522385,
                    0.72522385,
                    0.72522385,
                    0.72522385,
                    0.72522385,
                    0.72522385,
                    0.72279272,
                    0.72035872,
                    0.72035872,
                    0.72035872,
                    0.71790534,
                    0.71544907,
                    0.71298994,
                    0.71052796,
                    0.70806316,
                    0.70806316,
                    0.70806316,
                    0.70557832,
                    0.70309063,
                    0.70309063,
                    0.7005913,
                    0.69808913,
                    0.69558413,
                    0.69558413,
                    0.6930673,
                    0.69054766,
                    0.68802523,
                    0.68550002,
                    0.68297207,
                    0.68044139,
                    0.67790801,
                    0.67537194,
                    0.6728332,
                    0.67029182,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66774781,
                    0.66502561,
                    0.66502561,
                    0.66502561,
                    0.66502561,
                    0.66224336,
                    0.66224336,
                    0.65944567,
                    0.65944567,
                    0.65944567,
                    0.65944567,
                    0.65944567,
                    0.65659618,
                    0.65659618,
                    0.65659618,
                    0.65371784,
                    0.65371784,
                    0.65371784,
                    0.65371784,
                    0.65371784,
                    0.65371784,
                    0.65371784,
                    0.65371784,
                    0.65371784,
                    0.65070395,
                    0.65070395,
                    0.65070395,
                    0.65070395,
                    0.64764294,
                    0.64764294,
                    0.64764294,
                    0.64764294,
                    0.64453302,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.64138749,
                    0.63793702,
                    0.63793702,
                    0.63793702,
                    0.63793702,
                    0.63793702,
                    0.63793702,
                    0.63793702,
                    0.63793702,
                    0.63432516,
                    0.63432516,
                    0.63432516,
                    0.63432516,
                    0.63432516,
                    0.63432516,
                    0.63432516,
                    0.63432516,
                    0.63432516,
                    0.63432516,
                    0.63432516,
                    0.63432516,
                    0.63039956,
                    0.63039956,
                    0.63039956,
                    0.63039956,
                    0.63039956,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.62230531,
                    0.61765802,
                    0.61297663,
                    0.60826262,
                    0.60351733,
                    0.59874198,
                    0.59393766,
                    0.58910537,
                    0.58424603,
                    0.57936047,
                    0.57444944,
                    0.56951366,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.56455375,
                    0.55853254,
                    0.55853254,
                    0.55853254,
                    0.55853254,
                    0.55853254,
                    0.55853254,
                    0.55853254,
                    0.55853254,
                    0.55853254,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55187229,
                    0.55369988,
                    0.55369988,
                    0.55369988,
                    0.55369988,
                    0.55369988,
                    0.55369988,
                    0.55369988,
                    0.55369988,
                    0.55369988,
                    0.60116676,
                    0.52790258,
                    0.0,
                ],
            ]
        )
        return 0.99, "log-log", true_ci


@pytest.fixture()
def make_channing():
    def _make_channing(sex):  # pylint: disable=unused-argument
        data = pd.read_csv(CHANNING_FILE).query("entry < exit and sex == @sex")
        time_enter_m = data.loc[:, "entry"].to_numpy()
        time_exit_m = data.loc[:, "exit"].to_numpy()
        event_m = data.loc[:, "cens"].to_numpy() == 1
        return time_enter_m, time_exit_m, event_m

    return _make_channing


@pytest.fixture()
def channing_male_true_x():
    return np.array(
        [
            751,
            759,
            777,
            781,
            782,
            806,
            817,
            820,
            821,
            823,
            830,
            835,
            836,
            837,
            843,
            846,
            847,
            852,
            853,
            854,
            856,
            863,
            865,
            866,
            869,
            871,
            872,
            875,
            876,
            878,
            879,
            883,
            885,
            886,
            890,
            891,
            893,
            894,
            895,
            898,
            900,
            906,
            907,
            909,
            911,
            914,
            915,
            919,
            921,
            923,
            925,
            926,
            927,
            932,
            936,
            938,
            940,
            943,
            945,
            946,
            948,
            951,
            953,
            955,
            956,
            957,
            959,
            960,
            962,
            964,
            966,
            967,
            969,
            970,
            971,
            972,
            973,
            977,
            978,
            981,
            982,
            983,
            984,
            985,
            988,
            989,
            993,
            996,
            998,
            1001,
            1002,
            1005,
            1006,
            1007,
            1009,
            1010,
            1012,
            1013,
            1015,
            1016,
            1018,
            1020,
            1021,
            1022,
            1023,
            1025,
            1027,
            1029,
            1031,
            1033,
            1036,
            1039,
            1041,
            1043,
            1044,
            1045,
            1046,
            1047,
            1051,
            1053,
            1055,
            1058,
            1059,
            1060,
            1063,
            1064,
            1070,
            1073,
            1080,
            1085,
            1093,
            1094,
            1106,
            1107,
            1118,
            1128,
            1139,
            1153,
        ]
    )


@pytest.fixture()
def channing_female_true_x():
    return np.array(
        [
            733,
            746,
            748,
            760,
            762,
            768,
            769,
            772,
            775,
            777,
            783,
            792,
            794,
            795,
            796,
            797,
            798,
            799,
            802,
            804,
            805,
            807,
            808,
            809,
            810,
            811,
            812,
            813,
            814,
            815,
            818,
            819,
            820,
            821,
            822,
            823,
            824,
            825,
            827,
            828,
            829,
            830,
            831,
            833,
            834,
            835,
            836,
            837,
            838,
            839,
            840,
            841,
            842,
            843,
            844,
            845,
            846,
            847,
            848,
            849,
            850,
            851,
            852,
            854,
            855,
            856,
            857,
            858,
            859,
            860,
            861,
            863,
            864,
            865,
            866,
            867,
            868,
            869,
            870,
            871,
            872,
            873,
            874,
            875,
            876,
            877,
            878,
            881,
            882,
            883,
            885,
            886,
            887,
            888,
            889,
            890,
            891,
            892,
            893,
            894,
            895,
            896,
            897,
            898,
            899,
            900,
            901,
            902,
            904,
            905,
            906,
            907,
            908,
            909,
            910,
            911,
            912,
            913,
            914,
            915,
            916,
            917,
            918,
            919,
            920,
            921,
            922,
            923,
            924,
            925,
            926,
            927,
            928,
            929,
            930,
            931,
            932,
            933,
            934,
            935,
            936,
            938,
            939,
            940,
            941,
            942,
            943,
            944,
            945,
            946,
            947,
            948,
            950,
            951,
            952,
            953,
            954,
            955,
            956,
            957,
            958,
            959,
            960,
            961,
            962,
            963,
            964,
            965,
            966,
            967,
            968,
            969,
            970,
            971,
            972,
            973,
            975,
            976,
            977,
            978,
            979,
            981,
            982,
            983,
            984,
            985,
            986,
            987,
            988,
            989,
            990,
            991,
            992,
            993,
            994,
            995,
            996,
            997,
            998,
            999,
            1000,
            1001,
            1002,
            1003,
            1004,
            1005,
            1006,
            1007,
            1008,
            1009,
            1010,
            1011,
            1012,
            1013,
            1014,
            1015,
            1016,
            1017,
            1018,
            1019,
            1020,
            1021,
            1022,
            1023,
            1024,
            1026,
            1027,
            1028,
            1029,
            1030,
            1031,
            1032,
            1033,
            1034,
            1035,
            1036,
            1037,
            1038,
            1039,
            1040,
            1041,
            1042,
            1043,
            1044,
            1047,
            1049,
            1050,
            1051,
            1053,
            1054,
            1055,
            1056,
            1057,
            1059,
            1061,
            1062,
            1063,
            1064,
            1065,
            1068,
            1070,
            1071,
            1072,
            1073,
            1074,
            1080,
            1083,
            1084,
            1085,
            1086,
            1088,
            1089,
            1091,
            1093,
            1096,
            1097,
            1102,
            1105,
            1109,
            1114,
            1115,
            1119,
            1122,
            1131,
            1132,
            1134,
            1140,
            1142,
            1147,
            1152,
            1172,
            1186,
            1192,
            1200,
            1207,
        ]
    )


@pytest.fixture()
def make_aids():
    def _make_aids(kind):
        if kind == "children":
            f = AIDS_CHILDREN_FILE
        elif kind == "adults":
            f = AIDS_ADULTS_FILE
        else:
            raise ValueError(f"unknown kind {kind!r}")
        data = pd.read_csv(f, comment="#").query("(8 - DIAG) > INF")

        event = np.repeat(True, data.shape[0])
        time_enter = data["INF"]
        time_exit = 8 - data["DIAG"]
        return event, time_enter, time_exit

    return _make_aids


@pytest.fixture()
def truncated_failure_data():
    rnd = np.random.default_rng(2016)
    time_exit = rnd.uniform(1, 100, size=25)
    time_enter = time_exit + 1
    event = rnd.binomial(1, 0.6, size=25).astype(bool)
    return event, time_exit, time_enter


@pytest.fixture()
def random_survival_data():
    event = np.ones(10, dtype=bool)
    event[:5] = False
    rng = np.random.default_rng()
    return Surv.from_arrays(event=event, time=rng.random(10))


@pytest.fixture()
def whas500_true_x():
    return np.array(
        [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            10,
            11,
            14,
            16,
            17,
            18,
            19,
            20,
            22,
            26,
            31,
            32,
            33,
            34,
            37,
            42,
            46,
            49,
            52,
            53,
            55,
            57,
            60,
            61,
            62,
            64,
            69,
            76,
            81,
            83,
            88,
            91,
            93,
            95,
            97,
            100,
            101,
            108,
            109,
            113,
            116,
            117,
            118,
            129,
            132,
            134,
            135,
            137,
            140,
            143,
            145,
            146,
            151,
            166,
            169,
            187,
            192,
            197,
            200,
            226,
            233,
            235,
            259,
            269,
            274,
            287,
            289,
            295,
            297,
            312,
            313,
            321,
            328,
            343,
            345,
            354,
            358,
            359,
            363,
            368,
            371,
            373,
            376,
            382,
            385,
            386,
            390,
            392,
            397,
            398,
            399,
            400,
            403,
            405,
            406,
            407,
            408,
            411,
            412,
            416,
            418,
            419,
            421,
            422,
            424,
            426,
            427,
            433,
            437,
            440,
            442,
            445,
            446,
            449,
            450,
            451,
            452,
            457,
            458,
            459,
            465,
            466,
            467,
            473,
            475,
            478,
            479,
            480,
            486,
            497,
            506,
            507,
            510,
            511,
            516,
            519,
            521,
            522,
            523,
            524,
            529,
            530,
            532,
            535,
            537,
            542,
            544,
            550,
            551,
            552,
            554,
            559,
            562,
            568,
            570,
            573,
            578,
            587,
            589,
            606,
            609,
            612,
            614,
            626,
            631,
            632,
            644,
            646,
            649,
            654,
            659,
            662,
            670,
            673,
            675,
            704,
            714,
            718,
            725,
            849,
            865,
            903,
            905,
            920,
            936,
            953,
            1048,
            1054,
            1065,
            1096,
            1098,
            1102,
            1103,
            1105,
            1106,
            1107,
            1108,
            1109,
            1114,
            1117,
            1121,
            1123,
            1125,
            1126,
            1136,
            1140,
            1150,
            1151,
            1152,
            1157,
            1159,
            1160,
            1161,
            1162,
            1163,
            1165,
            1169,
            1170,
            1174,
            1178,
            1182,
            1187,
            1189,
            1190,
            1191,
            1196,
            1199,
            1200,
            1203,
            1207,
            1211,
            1217,
            1223,
            1224,
            1231,
            1232,
            1233,
            1234,
            1235,
            1244,
            1245,
            1248,
            1251,
            1253,
            1256,
            1257,
            1262,
            1265,
            1266,
            1272,
            1273,
            1274,
            1277,
            1279,
            1280,
            1290,
            1295,
            1298,
            1302,
            1308,
            1314,
            1317,
            1319,
            1320,
            1325,
            1329,
            1332,
            1333,
            1336,
            1338,
            1346,
            1347,
            1353,
            1359,
            1363,
            1365,
            1366,
            1374,
            1377,
            1378,
            1381,
            1384,
            1385,
            1388,
            1390,
            1400,
            1408,
            1409,
            1420,
            1430,
            1433,
            1438,
            1444,
            1449,
            1451,
            1454,
            1456,
            1458,
            1496,
            1506,
            1527,
            1536,
            1548,
            1553,
            1576,
            1577,
            1579,
            1624,
            1627,
            1671,
            1831,
            1836,
            1847,
            1854,
            1858,
            1863,
            1880,
            1883,
            1885,
            1887,
            1889,
            1893,
            1899,
            1904,
            1914,
            1919,
            1920,
            1923,
            1926,
            1931,
            1933,
            1934,
            1936,
            1939,
            1940,
            1941,
            1942,
            1954,
            1955,
            1964,
            1969,
            1976,
            1977,
            1979,
            1993,
            1994,
            2006,
            2009,
            2025,
            2032,
            2048,
            2057,
            2061,
            2064,
            2065,
            2066,
            2083,
            2084,
            2086,
            2100,
            2108,
            2113,
            2114,
            2118,
            2122,
            2123,
            2125,
            2126,
            2131,
            2132,
            2139,
            2145,
            2146,
            2151,
            2152,
            2156,
            2160,
            2166,
            2168,
            2172,
            2173,
            2175,
            2178,
            2190,
            2192,
            2350,
            2353,
            2358,
        ]
    )


class TestKaplanMeier:
    @staticmethod
    @pytest.mark.parametrize("time,event,true_x,true_y,km_ci", SimpleDataKMCases().get_cases())
    def test_simple(time, event, true_x, true_y, km_ci):
        x, y = kaplan_meier_estimator(event, time)

        assert_array_equal(x, true_x)
        assert_array_almost_equal(y, true_y)

        x, y, ci = kaplan_meier_estimator(event, time, conf_type="log-log")
        assert_array_equal(x, true_x)
        assert_array_almost_equal(y, true_y)
        assert_array_almost_equal(ci, km_ci)

        ys = Surv.from_arrays(event, time)
        est = SurvivalFunctionEstimator(conf_type="log-log").fit(ys)
        assert_array_equal(est.unique_time_[1:], true_x)
        assert_array_almost_equal(est.prob_[1:], true_y)
        assert_array_almost_equal(est.conf_int_[:, 1:], km_ci)
        prob, ci = est.predict_proba(true_x, return_conf_int=True)
        assert_array_almost_equal(prob, true_y)
        assert_array_almost_equal(ci, km_ci)

    @staticmethod
    @pytest.mark.parametrize("time,event,true_x,true_y,km_var", SimpleDataKMCases().get_cases())
    def test_wrong_dtype(time, event, true_x, true_y, km_var):  # noqa: F821 undefined name
        ys = Surv.from_arrays(event, time)
        est = SurvivalFunctionEstimator().fit(ys)
        with pytest.raises(ValueError, match="dtype='numeric' is not compatible with arrays of bytes/strings"):
            est.predict_proba(np.array(["should", "not", "work"]))

        rng = np.random.default_rng()
        with pytest.raises(
            ValueError,
            match=r"Found array with dim 3(\. SurvivalFunctionEstimator expected <= 2"
            r"|, while dim <= 2 is required by SurvivalFunctionEstimator)\.",
        ):
            est.predict_proba(rng.standard_normal((10, 9, 5)))

    @staticmethod
    @pytest.mark.parametrize("conf_level", [None, -1, 1.0, 3.0, np.inf, np.nan])
    def test_estimator_invalid_conf_level(random_survival_data, conf_level):
        msg = r"The 'conf_level' parameter of SurvivalFunctionEstimator must be a float in the range \(0\.0, 1\.0\)\."
        with pytest.raises(ValueError, match=msg):
            SurvivalFunctionEstimator(conf_level=conf_level, conf_type="log-log").fit(random_survival_data)

    @staticmethod
    @pytest.mark.parametrize("conf_level", [None, -1, 1.0, 3.0, np.inf, np.nan])
    def test_invalid_conf_level(random_survival_data, conf_level):
        msg = r"conf_level must be a float in the range \(0\.0, 1\.0\)"
        with pytest.raises(ValueError, match=msg):
            kaplan_meier_estimator(
                random_survival_data["event"],
                random_survival_data["time"],
                conf_level=conf_level,
                conf_type="log-log",
            )

    @staticmethod
    @pytest.mark.parametrize("conf_type", ["None", -1, "", "not"])
    def test_estimator_invalid_conf_type(random_survival_data, conf_type):
        msg = r"The 'conf_type' parameter of SurvivalFunctionEstimator must be None or a str among \{.+\}\."
        with pytest.raises(ValueError, match=msg):
            SurvivalFunctionEstimator(conf_type=conf_type).fit(random_survival_data)

    @staticmethod
    @pytest.mark.parametrize("conf_type", ["None", -1, "", "not"])
    def test_invalid_conf_type(random_survival_data, conf_type):
        msg = r"conf_type must be None or a str among \{.+\}"
        with pytest.raises(ValueError, match=msg):
            kaplan_meier_estimator(
                random_survival_data["event"],
                random_survival_data["time"],
                conf_type=conf_type,
            )

    @staticmethod
    def test_estimator_fit_without_ci(random_survival_data):
        est = SurvivalFunctionEstimator().fit(random_survival_data)
        msg = "If return_conf_int is True, SurvivalFunctionEstimator must be fitted with conf_int != None"
        with pytest.raises(ValueError, match=msg):
            est.predict_proba(np.unique(random_survival_data["time"]), return_conf_int=True)

    @staticmethod
    def test_truncated_enter_larger_exit_error(truncated_failure_data):
        with pytest.raises(ValueError, match="exit time must be larger start time for all samples"):
            kaplan_meier_estimator(*truncated_failure_data)

    @staticmethod
    def test_whas500(make_whas500, whas500_true_x):
        whas500 = make_whas500(with_mean=False, with_std=False)
        time = whas500.y["lenfol"]
        event = whas500.y["fstat"]

        x, y = kaplan_meier_estimator(event, time)

        assert_array_equal(x.astype(int), whas500_true_x)

        true_y = np.array(
            [
                0.984,
                0.968,
                0.962,
                0.958,
                0.954,
                0.944,
                0.932,
                0.926,
                0.918,
                0.914,
                0.912,
                0.908,
                0.902,
                0.896,
                0.892,
                0.888,
                0.886,
                0.884,
                0.88,
                0.874,
                0.872,
                0.87,
                0.868,
                0.866,
                0.864,
                0.862,
                0.86,
                0.858,
                0.854,
                0.852,
                0.85,
                0.848,
                0.844,
                0.84,
                0.838,
                0.836,
                0.834,
                0.832,
                0.83,
                0.828,
                0.826,
                0.824,
                0.822,
                0.82,
                0.818,
                0.816,
                0.814,
                0.812,
                0.81,
                0.808,
                0.806,
                0.804,
                0.802,
                0.8,
                0.798,
                0.794,
                0.792,
                0.79,
                0.788,
                0.786,
                0.784,
                0.78,
                0.776,
                0.774,
                0.772,
                0.77,
                0.768,
                0.766,
                0.764,
                0.76,
                0.758,
                0.756,
                0.754,
                0.752,
                0.75,
                0.746,
                0.744,
                0.742,
                0.74,
                0.738,
                0.736,
                0.734,
                0.732,
                0.73,
                0.726,
                0.724,
                0.724,
                0.724,
                0.724,
                0.724,
                0.721960563380282,
                0.719921126760564,
                0.719921126760564,
                0.719921126760564,
                0.717864209255533,
                0.715807291750503,
                0.715807291750503,
                0.715807291750503,
                0.715807291750503,
                0.715807291750503,
                0.713714287973455,
                0.711621284196407,
                0.711621284196407,
                0.711621284196407,
                0.711621284196407,
                0.711621284196407,
                0.711621284196407,
                0.711621284196407,
                0.709484283342964,
                0.709484283342964,
                0.70734082629359,
                0.70734082629359,
                0.70734082629359,
                0.70734082629359,
                0.70734082629359,
                0.70734082629359,
                0.70734082629359,
                0.705150916614662,
                0.705150916614662,
                0.702926465773606,
                0.702926465773606,
                0.702926465773606,
                0.702926465773606,
                0.702926465773606,
                0.702926465773606,
                0.702926465773606,
                0.702926465773606,
                0.700621788836644,
                0.700621788836644,
                0.69830950570517,
                0.695997222573696,
                0.695997222573696,
                0.695997222573696,
                0.693669472665422,
                0.693669472665422,
                0.693669472665422,
                0.691325994717228,
                0.691325994717228,
                0.691325994717228,
                0.691325994717228,
                0.691325994717228,
                0.691325994717228,
                0.691325994717228,
                0.691325994717228,
                0.691325994717228,
                0.691325994717228,
                0.691325994717228,
                0.691325994717228,
                0.688865759860583,
                0.688865759860583,
                0.686387825472596,
                0.683900913061463,
                0.68141400065033,
                0.68141400065033,
                0.68141400065033,
                0.68141400065033,
                0.678871411095665,
                0.678871411095665,
                0.676319262933651,
                0.673767114771638,
                0.673767114771638,
                0.673767114771638,
                0.673767114771638,
                0.673767114771638,
                0.673767114771638,
                0.673767114771638,
                0.673767114771638,
                0.673767114771638,
                0.671114488335529,
                0.66846186189942,
                0.66846186189942,
                0.66846186189942,
                0.665788014451822,
                0.663114167004225,
                0.660440319556627,
                0.657766472109029,
                0.655092624661431,
                0.655092624661431,
                0.655092624661431,
                0.652396770238956,
                0.649700915816481,
                0.649700915816481,
                0.646993828667246,
                0.644286741518011,
                0.641579654368776,
                0.641579654368776,
                0.638861096511281,
                0.636142538653786,
                0.633423980796291,
                0.630705422938796,
                0.627986865081301,
                0.625268307223807,
                0.622549749366312,
                0.619831191508817,
                0.617112633651322,
                0.614394075793827,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.611675517936333,
                0.608734770253946,
                0.608734770253946,
                0.608734770253946,
                0.608734770253946,
                0.605721231787343,
                0.605721231787343,
                0.602692625628406,
                0.602692625628406,
                0.602692625628406,
                0.602692625628406,
                0.602692625628406,
                0.599601894214927,
                0.599601894214927,
                0.599601894214927,
                0.596478967682557,
                0.596478967682557,
                0.596478967682557,
                0.596478967682557,
                0.596478967682557,
                0.596478967682557,
                0.596478967682557,
                0.596478967682557,
                0.596478967682557,
                0.593183503772709,
                0.593183503772709,
                0.593183503772709,
                0.593183503772709,
                0.589832184542355,
                0.589832184542355,
                0.589832184542355,
                0.589832184542355,
                0.586422749949625,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.582973204361686,
                0.579112454663926,
                0.579112454663926,
                0.579112454663926,
                0.579112454663926,
                0.579112454663926,
                0.579112454663926,
                0.579112454663926,
                0.579112454663926,
                0.575034197940941,
                0.575034197940941,
                0.575034197940941,
                0.575034197940941,
                0.575034197940941,
                0.575034197940941,
                0.575034197940941,
                0.575034197940941,
                0.575034197940941,
                0.575034197940941,
                0.575034197940941,
                0.575034197940941,
                0.570506369610697,
                0.570506369610697,
                0.570506369610697,
                0.570506369610697,
                0.570506369610697,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.561153806174456,
                0.555542268112711,
                0.549930730050967,
                0.544319191989222,
                0.538707653927478,
                0.533096115865733,
                0.527484577803989,
                0.521873039742244,
                0.5162615016805,
                0.510649963618755,
                0.505038425557011,
                0.499426887495266,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.493815349433521,
                0.486444971083767,
                0.486444971083767,
                0.486444971083767,
                0.486444971083767,
                0.486444971083767,
                0.486444971083767,
                0.486444971083767,
                0.486444971083767,
                0.486444971083767,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.477910848784052,
                0.438084944718714,
                0.438084944718714,
                0.438084944718714,
                0.438084944718714,
                0.438084944718714,
                0.438084944718714,
                0.438084944718714,
                0.438084944718714,
                0.438084944718714,
                0.292056629812476,
                0.146028314906238,
                0,
            ]
        )

        assert_array_almost_equal(y, true_y)

    @staticmethod
    @pytest.mark.parametrize("conf_level,conf_type,true_ci", Whas500CIData().get_cases())
    def test_ci_whas500(make_whas500, conf_level, conf_type, true_ci):
        whas500 = make_whas500(with_mean=False, with_std=False)
        time = whas500.y["lenfol"]
        event = whas500.y["fstat"]

        _, _, ci = kaplan_meier_estimator(event, time, conf_level=conf_level, conf_type=conf_type)

        assert_array_almost_equal(ci, true_ci)

    @staticmethod
    def test_left_truncated_simple1():
        time_enter = np.array([4, 3, 4, 2, 1, 5, 6, 7])
        time_exit = np.array([6, 7, 8, 10, 9, 9, 10, 11])
        event = np.array([0, 1, 0, 0, 1, 1, 1, 0], dtype=bool)

        x, y, km_ci = kaplan_meier_estimator(event, time_exit, time_enter, conf_type="log-log")

        true_x = np.arange(1, 12)
        assert_array_almost_equal(x, true_x)

        true_y = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.833333, 0.833333, 0.5, 0.333333, 0.333333])
        assert_array_almost_equal(y, true_y)

        true_ci = np.array(
            [
                [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.273123, 0.273123, 0.110948, 0.046082, 0.046082],
                [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.974712, 0.974712, 0.803709, 0.675564, 0.675564],
            ]
        )
        assert_array_almost_equal(km_ci, true_ci)

    @staticmethod
    def test_left_truncated_simple2():
        time_enter = np.array([1, 4, 3, 4, 2, 1, 5, 6, 7])
        time_exit = np.array([4, 6, 7, 8, 10, 9, 9, 10, 11])
        event = np.array([1, 0, 1, 0, 0, 1, 1, 1, 0], dtype=bool)

        x, y, km_ci = kaplan_meier_estimator(event, time_exit, time_enter, conf_type="log-log")

        true_x = np.arange(1, 12)
        assert_array_almost_equal(x, true_x)

        true_y = np.array([1.0, 1.0, 1.0, 0.75, 0.75, 0.75, 0.625, 0.625, 0.375, 0.25, 0.25])
        assert_array_almost_equal(y, true_y)

        true_ci = np.array(
            [
                [1.0, 1.0, 1.0, 0.127947, 0.127947, 0.127947, 0.141853, 0.141853, 0.069678, 0.03165, 0.03165],
                [1.0, 1.0, 1.0, 0.960549, 0.960549, 0.960549, 0.893051, 0.893051, 0.696882, 0.573177, 0.573177],
            ]
        )
        assert_array_almost_equal(km_ci, true_ci)

    @staticmethod
    def test_left_truncated_simple3():
        time_enter = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 9])
        time_exit = np.array([11, 12, 13, 14, 15, 16, 17, 18, 19, 19])
        event = np.array([0, 1, 0, 0, 1, 1, 1, 1, 0, 1], dtype=bool)

        x, y, km_ci = kaplan_meier_estimator(event, time_exit, time_enter, conf_type="log-log")

        true_x = np.concatenate((np.arange(1, 10), np.arange(11, 20)))
        assert_array_almost_equal(x, true_x)

        true_y = np.array([1, 0.888889, 0.888889, 0.888889, 0.740741, 0.592593, 0.444444, 0.296296, 0.148148])
        assert_array_almost_equal(y[9:], true_y)

        true_ci = np.array(
            [
                [1.0, 0.432965, 0.432965, 0.432965, 0.289212, 0.185919, 0.103865, 0.043129, 0.00736],
                [1.0, 0.983564, 0.983564, 0.983564, 0.929976, 0.849818, 0.747981, 0.624575, 0.475977],
            ]
        )
        assert_array_almost_equal(km_ci[:, :9], np.ones((2, 9)))
        assert_array_almost_equal(km_ci[:, 9:], true_ci)

    @staticmethod
    def test_truncated_male(make_channing, channing_male_true_x):
        time_enter_m, time_exit_m, event_m = make_channing("Male")

        x, y, km_ci = kaplan_meier_estimator(event_m, time_exit_m, time_enter_m, conf_type="log-log")

        assert_array_equal(x, channing_male_true_x)

        assert_array_equal(y[:3], np.array([1.0, 1.0, 0.5]))
        assert (y[3:] == 0).all()

        assert_array_almost_equal(
            km_ci[:, :3],
            np.array(
                [
                    [
                        1.0,
                        1.0,
                        0.00598309,
                    ],
                    [
                        1.0,
                        1.0,
                        0.91041008,
                    ],
                ]
            ),
        )

    @staticmethod
    def test_truncated_male_older_68(make_channing, channing_male_true_x):
        time_enter_m, time_exit_m, event_m = make_channing("Male")

        x, y, km_ci = kaplan_meier_estimator(event_m, time_exit_m, time_enter_m, time_min=68 * 12, conf_type="log-log")

        x_true = channing_male_true_x[6:]

        assert_array_equal(x, x_true)

        assert (y[:18] == 1).all()

        y_true = np.array(
            [
                0.95833333,
                0.95833333,
                0.920,
                0.920,
                0.88320,
                0.88320,
                0.88320,
                0.88320,
                0.88320,
                0.88320,
                0.88320,
                0.88320,
                0.85643636,
                0.83048375,
                0.83048375,
                0.80453113,
                0.80453113,
                0.80453113,
                0.78086845,
                0.75720577,
                0.73426014,
                0.73426014,
                0.73426014,
                0.73426014,
                0.73426014,
                0.73426014,
                0.73426014,
                0.73426014,
                0.71441527,
                0.69457040,
                0.69457040,
                0.69457040,
                0.69457040,
                0.69457040,
                0.67527678,
                0.67527678,
                0.65598316,
                0.65598316,
                0.65598316,
                0.65598316,
                0.65598316,
                0.63776140,
                0.63776140,
                0.63776140,
                0.63776140,
                0.63776140,
                0.62052461,
                0.62052461,
                0.60419501,
                0.60419501,
                0.58829514,
                0.58829514,
                0.58829514,
                0.58829514,
                0.58829514,
                0.58829514,
                0.58829514,
                0.57321065,
                0.57321065,
                0.55851294,
                0.55851294,
                0.54419210,
                0.51555041,
                0.51555041,
                0.50082040,
                0.50082040,
                0.50082040,
                0.50082040,
                0.50082040,
                0.50082040,
                0.48466490,
                0.48466490,
                0.45437335,
                0.45437335,
                0.45437335,
                0.45437335,
                0.45437335,
                0.45437335,
                0.45437335,
                0.43814573,
                0.43814573,
                0.42129397,
                0.42129397,
                0.40444221,
                0.38759045,
                0.36913376,
                0.35067707,
                0.35067707,
                0.35067707,
                0.33473721,
                0.31800035,
                0.31800035,
                0.31800035,
                0.31800035,
                0.31800035,
                0.30033366,
                0.28266697,
                0.28266697,
                0.26382251,
                0.24497804,
                0.24497804,
                0.24497804,
                0.24497804,
                0.24497804,
                0.22270731,
                0.20043658,
                0.20043658,
                0.15032744,
                0.15032744,
                0.15032744,
                0.15032744,
                0.10021829,
                0.05010915,
                0.05010915,
            ]
        )
        assert_array_almost_equal(y[18:], y_true)

        km_var_true = np.array(
            [
                [
                    0.73920615,
                    0.73920615,
                    0.71639034,
                    0.71639034,
                    0.68030206,
                    0.68030206,
                    0.68030206,
                    0.68030206,
                    0.68030206,
                    0.68030206,
                    0.68030206,
                    0.68030206,
                    0.65976749,
                    0.63750998,
                    0.63750998,
                    0.61378156,
                    0.61378156,
                    0.61378156,
                    0.59262859,
                    0.57096047,
                    0.54998014,
                    0.54998014,
                    0.54998014,
                    0.54998014,
                    0.54998014,
                    0.54998014,
                    0.54998014,
                    0.54998014,
                    0.53275695,
                    0.51533230,
                    0.51533230,
                    0.51533230,
                    0.51533230,
                    0.51533230,
                    0.49843790,
                    0.49843790,
                    0.48145069,
                    0.48145069,
                    0.48145069,
                    0.48145069,
                    0.48145069,
                    0.46565617,
                    0.46565617,
                    0.46565617,
                    0.46565617,
                    0.46565617,
                    0.45092306,
                    0.45092306,
                    0.43714048,
                    0.43714048,
                    0.42377890,
                    0.42377890,
                    0.42377890,
                    0.42377890,
                    0.42377890,
                    0.42377890,
                    0.42377890,
                    0.41124825,
                    0.41124825,
                    0.39908967,
                    0.39908967,
                    0.38729481,
                    0.36365220,
                    0.36365220,
                    0.35139832,
                    0.35139832,
                    0.35139832,
                    0.35139832,
                    0.35139832,
                    0.35139832,
                    0.33765257,
                    0.33765257,
                    0.31239834,
                    0.31239834,
                    0.31239834,
                    0.31239834,
                    0.31239834,
                    0.31239834,
                    0.31239834,
                    0.29868502,
                    0.29868502,
                    0.28437251,
                    0.28437251,
                    0.27014867,
                    0.25601996,
                    0.24027913,
                    0.22471176,
                    0.22471176,
                    0.22471176,
                    0.21192818,
                    0.19843250,
                    0.19843250,
                    0.19843250,
                    0.19843250,
                    0.19843250,
                    0.18411660,
                    0.16997889,
                    0.16997889,
                    0.15483230,
                    0.13996103,
                    0.13996103,
                    0.13996103,
                    0.13996103,
                    0.13996103,
                    0.12185686,
                    0.10440547,
                    0.10440547,
                    0.06652640,
                    0.06652640,
                    0.06652640,
                    0.06652640,
                    0.02653795,
                    0.00474936,
                    0.00474936,
                ],
                [
                    0.99402372,
                    0.99402372,
                    0.97937059,
                    0.97937059,
                    0.96074504,
                    0.96074504,
                    0.96074504,
                    0.96074504,
                    0.96074504,
                    0.96074504,
                    0.96074504,
                    0.96074504,
                    0.94388370,
                    0.92622382,
                    0.92622382,
                    0.90763582,
                    0.90763582,
                    0.90763582,
                    0.88963893,
                    0.87108191,
                    0.85249376,
                    0.85249376,
                    0.85249376,
                    0.85249376,
                    0.85249376,
                    0.85249376,
                    0.85249376,
                    0.85249376,
                    0.83560540,
                    0.81842984,
                    0.81842984,
                    0.81842984,
                    0.81842984,
                    0.81842984,
                    0.80139052,
                    0.80139052,
                    0.78411879,
                    0.78411879,
                    0.78411879,
                    0.78411879,
                    0.78411879,
                    0.76743643,
                    0.76743643,
                    0.76743643,
                    0.76743643,
                    0.76743643,
                    0.75133510,
                    0.75133510,
                    0.73580135,
                    0.73580135,
                    0.72048374,
                    0.72048374,
                    0.72048374,
                    0.72048374,
                    0.72048374,
                    0.72048374,
                    0.72048374,
                    0.70572056,
                    0.70572056,
                    0.69117856,
                    0.69117856,
                    0.67686200,
                    0.64796535,
                    0.64796535,
                    0.63303724,
                    0.63303724,
                    0.63303724,
                    0.63303724,
                    0.63303724,
                    0.63303724,
                    0.61681870,
                    0.61681870,
                    0.58576866,
                    0.58576866,
                    0.58576866,
                    0.58576866,
                    0.58576866,
                    0.58576866,
                    0.58576866,
                    0.56919114,
                    0.56919114,
                    0.55198609,
                    0.55198609,
                    0.53465574,
                    0.51719803,
                    0.49831695,
                    0.47926018,
                    0.47926018,
                    0.47926018,
                    0.46209746,
                    0.44413886,
                    0.44413886,
                    0.44413886,
                    0.44413886,
                    0.44413886,
                    0.42526910,
                    0.40622026,
                    0.40622026,
                    0.38604558,
                    0.36562364,
                    0.36562364,
                    0.36562364,
                    0.36562364,
                    0.36562364,
                    0.34244801,
                    0.31876245,
                    0.31876245,
                    0.26581883,
                    0.26581883,
                    0.26581883,
                    0.26581883,
                    0.23266803,
                    0.18728970,
                    0.18728970,
                ],
            ]
        )

        assert_array_almost_equal(km_ci[:, :18], np.ones((2, 18)))
        assert_array_almost_equal(km_ci[:, 18:], km_var_true)

    @staticmethod
    def test_truncated_female(make_channing, channing_female_true_x):
        time_enter_f, time_exit_f, event_f = make_channing("Female")

        x, y, km_ci = kaplan_meier_estimator(event_f, time_exit_f, time_enter_f, conf_type="log-log")

        assert_array_equal(x, channing_female_true_x)

        assert (y[:19] == 1).all()

        y_true = np.array(
            [
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.952381,
                0.925926,
                0.925926,
                0.925926,
                0.925926,
                0.925926,
                0.925926,
                0.925926,
                0.905797,
                0.905797,
                0.905797,
                0.905797,
                0.905797,
                0.905797,
                0.905797,
                0.905797,
                0.905797,
                0.890180,
                0.890180,
                0.890180,
                0.890180,
                0.890180,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.876692,
                0.866842,
                0.866842,
                0.866842,
                0.866842,
                0.866842,
                0.866842,
                0.858086,
                0.858086,
                0.858086,
                0.858086,
                0.858086,
                0.849914,
                0.849914,
                0.849914,
                0.849914,
                0.849914,
                0.849914,
                0.849914,
                0.849914,
                0.842523,
                0.835383,
                0.835383,
                0.835383,
                0.835383,
                0.835383,
                0.835383,
                0.835383,
                0.835383,
                0.835383,
                0.835383,
                0.829241,
                0.829241,
                0.823275,
                0.823275,
                0.823275,
                0.823275,
                0.817558,
                0.817558,
                0.817558,
                0.806510,
                0.806510,
                0.806510,
                0.795537,
                0.795537,
                0.795537,
                0.790233,
                0.790233,
                0.790233,
                0.790233,
                0.784929,
                0.784929,
                0.784929,
                0.784929,
                0.779697,
                0.779697,
                0.779697,
                0.779697,
                0.774762,
                0.774762,
                0.774762,
                0.769920,
                0.769920,
                0.765016,
                0.765016,
                0.760143,
                0.755301,
                0.750490,
                0.750490,
                0.745648,
                0.745648,
                0.741102,
                0.741102,
                0.741102,
                0.736527,
                0.731924,
                0.731924,
                0.731924,
                0.723105,
                0.723105,
                0.723105,
                0.723105,
                0.718586,
                0.718586,
                0.718586,
                0.718586,
                0.718586,
                0.714067,
                0.714067,
                0.714067,
                0.714067,
                0.714067,
                0.709631,
                0.709631,
                0.709631,
                0.709631,
                0.705112,
                0.705112,
                0.705112,
                0.700562,
                0.700562,
                0.700562,
                0.691914,
                0.687589,
                0.687589,
                0.687589,
                0.687589,
                0.683237,
                0.678829,
                0.678829,
                0.674392,
                0.674392,
                0.674392,
                0.665577,
                0.661169,
                0.661169,
                0.661169,
                0.656641,
                0.656641,
                0.656641,
                0.652081,
                0.633582,
                0.628957,
                0.624332,
                0.624332,
                0.615014,
                0.605696,
                0.591610,
                0.591610,
                0.586877,
                0.582105,
                0.577334,
                0.572523,
                0.572523,
                0.567671,
                0.562819,
                0.558009,
                0.548304,
                0.548304,
                0.548304,
                0.548304,
                0.543320,
                0.538242,
                0.527891,
                0.522559,
                0.517333,
                0.511944,
                0.511944,
                0.506556,
                0.495778,
                0.485000,
                0.479360,
                0.473585,
                0.473585,
                0.461892,
                0.455893,
                0.455893,
                0.449972,
                0.449972,
                0.444203,
                0.438359,
                0.438359,
                0.438359,
                0.432185,
                0.432185,
                0.432185,
                0.432185,
                0.432185,
                0.432185,
                0.432185,
                0.406382,
                0.387031,
                0.387031,
                0.380580,
                0.374019,
                0.367340,
                0.367340,
                0.367340,
                0.367340,
                0.367340,
                0.367340,
                0.367340,
                0.352034,
                0.352034,
                0.352034,
                0.352034,
                0.352034,
                0.343652,
                0.335061,
                0.335061,
                0.317426,
                0.308609,
                0.308609,
                0.299791,
                0.290707,
                0.281622,
                0.281622,
                0.272235,
                0.262847,
                0.244073,
                0.234310,
                0.234310,
                0.224122,
                0.224122,
                0.213450,
                0.213450,
                0.203285,
                0.203285,
                0.203285,
                0.203285,
                0.203285,
                0.189733,
                0.189733,
                0.175138,
                0.160543,
                0.145949,
                0.145949,
                0.145949,
                0.131354,
                0.131354,
                0.114934,
                0.098515,
                0.098515,
                0.073886,
                0.024629,
                0.024629,
            ]
        )
        assert_array_almost_equal(y[19:], y_true)

        km_ci_true = np.array(
            [
                [
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.707207,
                    0.723846,
                    0.723846,
                    0.723846,
                    0.723846,
                    0.723846,
                    0.723846,
                    0.723846,
                    0.720466,
                    0.720466,
                    0.720466,
                    0.720466,
                    0.720466,
                    0.720466,
                    0.720466,
                    0.720466,
                    0.720466,
                    0.714723,
                    0.714723,
                    0.714723,
                    0.714723,
                    0.714723,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.708062,
                    0.703256,
                    0.703256,
                    0.703256,
                    0.703256,
                    0.703256,
                    0.703256,
                    0.698571,
                    0.698571,
                    0.698571,
                    0.698571,
                    0.698571,
                    0.693837,
                    0.693837,
                    0.693837,
                    0.693837,
                    0.693837,
                    0.693837,
                    0.693837,
                    0.693837,
                    0.689374,
                    0.684836,
                    0.684836,
                    0.684836,
                    0.684836,
                    0.684836,
                    0.684836,
                    0.684836,
                    0.684836,
                    0.684836,
                    0.684836,
                    0.680918,
                    0.680918,
                    0.676987,
                    0.676987,
                    0.676987,
                    0.676987,
                    0.673132,
                    0.673132,
                    0.673132,
                    0.665427,
                    0.665427,
                    0.665427,
                    0.657441,
                    0.657441,
                    0.657441,
                    0.653503,
                    0.653503,
                    0.653503,
                    0.653503,
                    0.649504,
                    0.649504,
                    0.649504,
                    0.649504,
                    0.645513,
                    0.645513,
                    0.645513,
                    0.645513,
                    0.641741,
                    0.641741,
                    0.641741,
                    0.638011,
                    0.638011,
                    0.634187,
                    0.634187,
                    0.630356,
                    0.626521,
                    0.622684,
                    0.622684,
                    0.618791,
                    0.618791,
                    0.615143,
                    0.615143,
                    0.615143,
                    0.611447,
                    0.607704,
                    0.607704,
                    0.607704,
                    0.600519,
                    0.600519,
                    0.600519,
                    0.600519,
                    0.596798,
                    0.596798,
                    0.596798,
                    0.596798,
                    0.596798,
                    0.593061,
                    0.593061,
                    0.593061,
                    0.593061,
                    0.593061,
                    0.589388,
                    0.589388,
                    0.589388,
                    0.589388,
                    0.585621,
                    0.585621,
                    0.585621,
                    0.581812,
                    0.581812,
                    0.581812,
                    0.574581,
                    0.570949,
                    0.570949,
                    0.570949,
                    0.570949,
                    0.567280,
                    0.563548,
                    0.563548,
                    0.559780,
                    0.559780,
                    0.559780,
                    0.552271,
                    0.548504,
                    0.548504,
                    0.548504,
                    0.544613,
                    0.544613,
                    0.544613,
                    0.540685,
                    0.524653,
                    0.520629,
                    0.516600,
                    0.516600,
                    0.508460,
                    0.500301,
                    0.487926,
                    0.487926,
                    0.483757,
                    0.479547,
                    0.475334,
                    0.471079,
                    0.471079,
                    0.466781,
                    0.462481,
                    0.458220,
                    0.449609,
                    0.449609,
                    0.449609,
                    0.449609,
                    0.445169,
                    0.440634,
                    0.431366,
                    0.426573,
                    0.421888,
                    0.417037,
                    0.417037,
                    0.412187,
                    0.402490,
                    0.392797,
                    0.387696,
                    0.382458,
                    0.382458,
                    0.371843,
                    0.366384,
                    0.366384,
                    0.361010,
                    0.361010,
                    0.355798,
                    0.350512,
                    0.350512,
                    0.350512,
                    0.344887,
                    0.344887,
                    0.344887,
                    0.344887,
                    0.344887,
                    0.344887,
                    0.344887,
                    0.321302,
                    0.303706,
                    0.303706,
                    0.297859,
                    0.291906,
                    0.285840,
                    0.285840,
                    0.285840,
                    0.285840,
                    0.285840,
                    0.285840,
                    0.285840,
                    0.271660,
                    0.271660,
                    0.271660,
                    0.271660,
                    0.271660,
                    0.263799,
                    0.255739,
                    0.255739,
                    0.239235,
                    0.231051,
                    0.231051,
                    0.222914,
                    0.214526,
                    0.206191,
                    0.206191,
                    0.197576,
                    0.189024,
                    0.172116,
                    0.163353,
                    0.163353,
                    0.154204,
                    0.154204,
                    0.144620,
                    0.144620,
                    0.135715,
                    0.135715,
                    0.135715,
                    0.135715,
                    0.135715,
                    0.123075,
                    0.123075,
                    0.109553,
                    0.096498,
                    0.083916,
                    0.083916,
                    0.083916,
                    0.071824,
                    0.071824,
                    0.058340,
                    0.045735,
                    0.045735,
                    0.026248,
                    0.002340,
                    0.002340,
                ],
                [
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    1,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.993152,
                    0.981840,
                    0.981840,
                    0.981840,
                    0.981840,
                    0.981840,
                    0.981840,
                    0.981840,
                    0.970583,
                    0.970583,
                    0.970583,
                    0.970583,
                    0.970583,
                    0.970583,
                    0.970583,
                    0.970583,
                    0.970583,
                    0.960507,
                    0.960507,
                    0.960507,
                    0.960507,
                    0.960507,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.951072,
                    0.943644,
                    0.943644,
                    0.943644,
                    0.943644,
                    0.943644,
                    0.943644,
                    0.936786,
                    0.936786,
                    0.936786,
                    0.936786,
                    0.936786,
                    0.930205,
                    0.930205,
                    0.930205,
                    0.930205,
                    0.930205,
                    0.930205,
                    0.930205,
                    0.930205,
                    0.924098,
                    0.918094,
                    0.918094,
                    0.918094,
                    0.918094,
                    0.918094,
                    0.918094,
                    0.918094,
                    0.918094,
                    0.918094,
                    0.918094,
                    0.912808,
                    0.912808,
                    0.907610,
                    0.907610,
                    0.907610,
                    0.907610,
                    0.902568,
                    0.902568,
                    0.902568,
                    0.892681,
                    0.892681,
                    0.892681,
                    0.882715,
                    0.882715,
                    0.882715,
                    0.877842,
                    0.877842,
                    0.877842,
                    0.877842,
                    0.872940,
                    0.872940,
                    0.872940,
                    0.872940,
                    0.868074,
                    0.868074,
                    0.868074,
                    0.868074,
                    0.863446,
                    0.863446,
                    0.863446,
                    0.858879,
                    0.858879,
                    0.854237,
                    0.854237,
                    0.849604,
                    0.844982,
                    0.840370,
                    0.840370,
                    0.835714,
                    0.835714,
                    0.831311,
                    0.831311,
                    0.831311,
                    0.826870,
                    0.822388,
                    0.822388,
                    0.822388,
                    0.813747,
                    0.813747,
                    0.813747,
                    0.813747,
                    0.809308,
                    0.809308,
                    0.809308,
                    0.809308,
                    0.809308,
                    0.804858,
                    0.804858,
                    0.804858,
                    0.804858,
                    0.804858,
                    0.800475,
                    0.800475,
                    0.800475,
                    0.800475,
                    0.796004,
                    0.796004,
                    0.796004,
                    0.791495,
                    0.791495,
                    0.791495,
                    0.782870,
                    0.778545,
                    0.778545,
                    0.778545,
                    0.778545,
                    0.774186,
                    0.769766,
                    0.769766,
                    0.765311,
                    0.765311,
                    0.765311,
                    0.756433,
                    0.751982,
                    0.751982,
                    0.751982,
                    0.747411,
                    0.747411,
                    0.747411,
                    0.742802,
                    0.724054,
                    0.719349,
                    0.714638,
                    0.714638,
                    0.705132,
                    0.695601,
                    0.681156,
                    0.681156,
                    0.676294,
                    0.671390,
                    0.666480,
                    0.661526,
                    0.661526,
                    0.656528,
                    0.651525,
                    0.646555,
                    0.636520,
                    0.636520,
                    0.636520,
                    0.636520,
                    0.631368,
                    0.626124,
                    0.615432,
                    0.609930,
                    0.604522,
                    0.598955,
                    0.598955,
                    0.593381,
                    0.582214,
                    0.571021,
                    0.565182,
                    0.559210,
                    0.559210,
                    0.547111,
                    0.540910,
                    0.540910,
                    0.534772,
                    0.534772,
                    0.528765,
                    0.522681,
                    0.522681,
                    0.522681,
                    0.516287,
                    0.516287,
                    0.516287,
                    0.516287,
                    0.516287,
                    0.516287,
                    0.516287,
                    0.489608,
                    0.469480,
                    0.469480,
                    0.462748,
                    0.455904,
                    0.448942,
                    0.448942,
                    0.448942,
                    0.448942,
                    0.448942,
                    0.448942,
                    0.448942,
                    0.433270,
                    0.433270,
                    0.433270,
                    0.433270,
                    0.433270,
                    0.424790,
                    0.416108,
                    0.416108,
                    0.398269,
                    0.389292,
                    0.389292,
                    0.380276,
                    0.371003,
                    0.361684,
                    0.361684,
                    0.352076,
                    0.342415,
                    0.322926,
                    0.312797,
                    0.312797,
                    0.302269,
                    0.302269,
                    0.291290,
                    0.291290,
                    0.280597,
                    0.280597,
                    0.280597,
                    0.280597,
                    0.280597,
                    0.267473,
                    0.267473,
                    0.253459,
                    0.239077,
                    0.224322,
                    0.224322,
                    0.224322,
                    0.209178,
                    0.209178,
                    0.192601,
                    0.175334,
                    0.175334,
                    0.154970,
                    0.103873,
                    0.103873,
                ],
            ]
        )
        assert_array_almost_equal(km_ci, km_ci_true)

    @staticmethod
    def test_truncated_female_older_68(make_channing, channing_female_true_x):
        time_enter_f, time_exit_f, event_f = make_channing("Female")

        x, y, km_ci = kaplan_meier_estimator(event_f, time_exit_f, time_enter_f, time_min=68 * 12, conf_type="log-log")

        x_true = channing_female_true_x[30:]
        assert_array_equal(x, x_true)

        y_true = np.array(
            [
                1.0,
                1.0,
                1.0,
                1.0,
                0.97222222,
                0.97222222,
                0.97222222,
                0.97222222,
                0.97222222,
                0.97222222,
                0.97222222,
                0.95108696,
                0.95108696,
                0.95108696,
                0.95108696,
                0.95108696,
                0.95108696,
                0.95108696,
                0.95108696,
                0.95108696,
                0.93468891,
                0.93468891,
                0.93468891,
                0.93468891,
                0.93468891,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.92052695,
                0.91018395,
                0.91018395,
                0.91018395,
                0.91018395,
                0.91018395,
                0.91018395,
                0.90099018,
                0.90099018,
                0.90099018,
                0.90099018,
                0.90099018,
                0.89240932,
                0.89240932,
                0.89240932,
                0.89240932,
                0.89240932,
                0.89240932,
                0.89240932,
                0.89240932,
                0.88464924,
                0.87715221,
                0.87715221,
                0.87715221,
                0.87715221,
                0.87715221,
                0.87715221,
                0.87715221,
                0.87715221,
                0.87715221,
                0.87715221,
                0.87070256,
                0.87070256,
                0.86443851,
                0.86443851,
                0.86443851,
                0.86443851,
                0.85843547,
                0.85843547,
                0.85843547,
                0.84683499,
                0.84683499,
                0.84683499,
                0.83531342,
                0.83531342,
                0.83531342,
                0.82974467,
                0.82974467,
                0.82974467,
                0.82974467,
                0.82417591,
                0.82417591,
                0.82417591,
                0.82417591,
                0.81868141,
                0.81868141,
                0.81868141,
                0.81868141,
                0.81349988,
                0.81349988,
                0.81349988,
                0.80841550,
                0.80841550,
                0.80326636,
                0.80326636,
                0.79815001,
                0.79306626,
                0.78801488,
                0.78801488,
                0.78293091,
                0.78293091,
                0.77815694,
                0.77815694,
                0.77815694,
                0.77335350,
                0.76852004,
                0.76852004,
                0.76852004,
                0.75926077,
                0.75926077,
                0.75926077,
                0.75926077,
                0.75451539,
                0.75451539,
                0.75451539,
                0.75451539,
                0.75451539,
                0.74977001,
                0.74977001,
                0.74977001,
                0.74977001,
                0.74977001,
                0.74511305,
                0.74511305,
                0.74511305,
                0.74511305,
                0.74036711,
                0.74036711,
                0.74036711,
                0.73559055,
                0.73559055,
                0.73559055,
                0.72650918,
                0.72196850,
                0.72196850,
                0.72196850,
                0.72196850,
                0.71739908,
                0.71277070,
                0.71277070,
                0.70811206,
                0.70811206,
                0.70811206,
                0.69885570,
                0.69422751,
                0.69422751,
                0.69422751,
                0.68947253,
                0.68947253,
                0.68947253,
                0.68468453,
                0.66526085,
                0.66040493,
                0.65554902,
                0.65554902,
                0.64576470,
                0.63598039,
                0.62119015,
                0.62119015,
                0.61622063,
                0.61121070,
                0.60620078,
                0.60114911,
                0.60114911,
                0.59605462,
                0.59096014,
                0.58590920,
                0.57571947,
                0.57571947,
                0.57571947,
                0.57571947,
                0.57048566,
                0.56515402,
                0.55428567,
                0.54868682,
                0.54319996,
                0.53754162,
                0.53754162,
                0.53188329,
                0.52056662,
                0.50924996,
                0.50332845,
                0.49726425,
                0.49726425,
                0.48498612,
                0.47868760,
                0.47868760,
                0.47247088,
                0.47247088,
                0.46641356,
                0.46027654,
                0.46027654,
                0.46027654,
                0.45379377,
                0.45379377,
                0.45379377,
                0.45379377,
                0.45379377,
                0.45379377,
                0.45379377,
                0.42670160,
                0.40638248,
                0.40638248,
                0.39960944,
                0.39271962,
                0.38570677,
                0.38570677,
                0.38570677,
                0.38570677,
                0.38570677,
                0.38570677,
                0.38570677,
                0.36963565,
                0.36963565,
                0.36963565,
                0.36963565,
                0.36963565,
                0.36083481,
                0.35181394,
                0.35181394,
                0.33329741,
                0.32403915,
                0.32403915,
                0.31478089,
                0.30524208,
                0.29570326,
                0.29570326,
                0.28584648,
                0.27598971,
                0.25627616,
                0.24602511,
                0.24602511,
                0.23532837,
                0.23532837,
                0.22412226,
                0.22412226,
                0.21344977,
                0.21344977,
                0.21344977,
                0.21344977,
                0.21344977,
                0.19921978,
                0.19921978,
                0.18389518,
                0.16857059,
                0.15324599,
                0.15324599,
                0.15324599,
                0.13792139,
                0.13792139,
                0.12068121,
                0.10344104,
                0.10344104,
                0.07758078,
                0.02586026,
                0.02586026,
            ]
        )
        assert_array_almost_equal(y, y_true)

        km_var_true = np.array(
            [
                [
                    1.0,
                    1.0,
                    1.0,
                    1.0,
                    0.81873073,
                    0.81873073,
                    0.81873073,
                    0.81873073,
                    0.81873073,
                    0.81873073,
                    0.81873073,
                    0.81656384,
                    0.81656384,
                    0.81656384,
                    0.81656384,
                    0.81656384,
                    0.81656384,
                    0.81656384,
                    0.81656384,
                    0.81656384,
                    0.80735677,
                    0.80735677,
                    0.80735677,
                    0.80735677,
                    0.80735677,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79706777,
                    0.79016506,
                    0.79016506,
                    0.79016506,
                    0.79016506,
                    0.79016506,
                    0.79016506,
                    0.78359570,
                    0.78359570,
                    0.78359570,
                    0.78359570,
                    0.78359570,
                    0.77708712,
                    0.77708712,
                    0.77708712,
                    0.77708712,
                    0.77708712,
                    0.77708712,
                    0.77708712,
                    0.77708712,
                    0.77107697,
                    0.76505042,
                    0.76505042,
                    0.76505042,
                    0.76505042,
                    0.76505042,
                    0.76505042,
                    0.76505042,
                    0.76505042,
                    0.76505042,
                    0.76505042,
                    0.75994158,
                    0.75994158,
                    0.75486496,
                    0.75486496,
                    0.75486496,
                    0.75486496,
                    0.74993227,
                    0.74993227,
                    0.74993227,
                    0.74018988,
                    0.74018988,
                    0.74018988,
                    0.73021918,
                    0.73021918,
                    0.73021918,
                    0.72534813,
                    0.72534813,
                    0.72534813,
                    0.72534813,
                    0.72042709,
                    0.72042709,
                    0.72042709,
                    0.72042709,
                    0.71554182,
                    0.71554182,
                    0.71554182,
                    0.71554182,
                    0.71095222,
                    0.71095222,
                    0.71095222,
                    0.70643261,
                    0.70643261,
                    0.70181511,
                    0.70181511,
                    0.69720598,
                    0.69260768,
                    0.68802237,
                    0.68802237,
                    0.68338274,
                    0.68338274,
                    0.67905471,
                    0.67905471,
                    0.67905471,
                    0.67467997,
                    0.67025905,
                    0.67025905,
                    0.67025905,
                    0.66180935,
                    0.66180935,
                    0.66180935,
                    0.66180935,
                    0.65744535,
                    0.65744535,
                    0.65744535,
                    0.65744535,
                    0.65744535,
                    0.65307116,
                    0.65307116,
                    0.65307116,
                    0.65307116,
                    0.65307116,
                    0.64878126,
                    0.64878126,
                    0.64878126,
                    0.64878126,
                    0.64438840,
                    0.64438840,
                    0.64438840,
                    0.63995502,
                    0.63995502,
                    0.63995502,
                    0.63156674,
                    0.62736299,
                    0.62736299,
                    0.62736299,
                    0.62736299,
                    0.62312314,
                    0.61881556,
                    0.61881556,
                    0.61447102,
                    0.61447102,
                    0.61447102,
                    0.60583368,
                    0.60150925,
                    0.60150925,
                    0.60150925,
                    0.59704632,
                    0.59704632,
                    0.59704632,
                    0.59254516,
                    0.57422730,
                    0.56964384,
                    0.56505925,
                    0.56505925,
                    0.55580983,
                    0.54655877,
                    0.53256033,
                    0.53256033,
                    0.52785266,
                    0.52310229,
                    0.51835301,
                    0.51355995,
                    0.51355995,
                    0.50872216,
                    0.50388615,
                    0.49909898,
                    0.48943583,
                    0.48943583,
                    0.48943583,
                    0.48943583,
                    0.48445737,
                    0.47937555,
                    0.46899916,
                    0.46363677,
                    0.45840196,
                    0.45298389,
                    0.45298389,
                    0.44757133,
                    0.43676308,
                    0.42597791,
                    0.42030520,
                    0.41448228,
                    0.41448228,
                    0.40269697,
                    0.39664110,
                    0.39664110,
                    0.39068687,
                    0.39068687,
                    0.38491873,
                    0.37907192,
                    0.37907192,
                    0.37907192,
                    0.37285153,
                    0.37285153,
                    0.37285153,
                    0.37285153,
                    0.37285153,
                    0.37285153,
                    0.37285153,
                    0.34681232,
                    0.32744838,
                    0.32744838,
                    0.32102576,
                    0.31448980,
                    0.30783467,
                    0.30783467,
                    0.30783467,
                    0.30783467,
                    0.30783467,
                    0.30783467,
                    0.30783467,
                    0.29227567,
                    0.29227567,
                    0.29227567,
                    0.29227567,
                    0.29227567,
                    0.28365662,
                    0.27482890,
                    0.27482890,
                    0.25678695,
                    0.24785939,
                    0.24785939,
                    0.23899403,
                    0.22986528,
                    0.22080728,
                    0.22080728,
                    0.21145278,
                    0.20218011,
                    0.18388277,
                    0.17441392,
                    0.17441392,
                    0.16453907,
                    0.16453907,
                    0.15420430,
                    0.15420430,
                    0.14461956,
                    0.14461956,
                    0.14461956,
                    0.14461956,
                    0.14461956,
                    0.13101115,
                    0.13101115,
                    0.11648197,
                    0.10248780,
                    0.08903091,
                    0.08903091,
                    0.08903091,
                    0.07612366,
                    0.07612366,
                    0.06175318,
                    0.04834884,
                    0.04834884,
                    0.02766766,
                    0.00244608,
                    0.00244608,
                ],
                [
                    1.0,
                    1.0,
                    1.0,
                    1.0,
                    0.99603987,
                    0.99603987,
                    0.99603987,
                    0.99603987,
                    0.99603987,
                    0.99603987,
                    0.99603987,
                    0.98766614,
                    0.98766614,
                    0.98766614,
                    0.98766614,
                    0.98766614,
                    0.98766614,
                    0.98766614,
                    0.98766614,
                    0.98766614,
                    0.97890750,
                    0.97890750,
                    0.97890750,
                    0.97890750,
                    0.97890750,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.97021938,
                    0.96309354,
                    0.96309354,
                    0.96309354,
                    0.96309354,
                    0.96309354,
                    0.96309354,
                    0.95640313,
                    0.95640313,
                    0.95640313,
                    0.95640313,
                    0.95640313,
                    0.94992074,
                    0.94992074,
                    0.94992074,
                    0.94992074,
                    0.94992074,
                    0.94992074,
                    0.94992074,
                    0.94992074,
                    0.94385376,
                    0.93786308,
                    0.93786308,
                    0.93786308,
                    0.93786308,
                    0.93786308,
                    0.93786308,
                    0.93786308,
                    0.93786308,
                    0.93786308,
                    0.93786308,
                    0.93255080,
                    0.93255080,
                    0.92731470,
                    0.92731470,
                    0.92731470,
                    0.92731470,
                    0.92222395,
                    0.92222395,
                    0.92222395,
                    0.91222399,
                    0.91222399,
                    0.91222399,
                    0.90213532,
                    0.90213532,
                    0.90213532,
                    0.89719642,
                    0.89719642,
                    0.89719642,
                    0.89719642,
                    0.89222913,
                    0.89222913,
                    0.89222913,
                    0.89222913,
                    0.88729603,
                    0.88729603,
                    0.88729603,
                    0.88729603,
                    0.88259747,
                    0.88259747,
                    0.88259747,
                    0.87795942,
                    0.87795942,
                    0.87324801,
                    0.87324801,
                    0.86854580,
                    0.86385386,
                    0.85917314,
                    0.85917314,
                    0.85444938,
                    0.85444938,
                    0.84997655,
                    0.84997655,
                    0.84997655,
                    0.84546565,
                    0.84091656,
                    0.84091656,
                    0.84091656,
                    0.83213827,
                    0.83213827,
                    0.83213827,
                    0.83213827,
                    0.82763256,
                    0.82763256,
                    0.82763256,
                    0.82763256,
                    0.82763256,
                    0.82311658,
                    0.82311658,
                    0.82311658,
                    0.82311658,
                    0.82311658,
                    0.81866816,
                    0.81866816,
                    0.81866816,
                    0.81866816,
                    0.81413240,
                    0.81413240,
                    0.81413240,
                    0.80956052,
                    0.80956052,
                    0.80956052,
                    0.80080655,
                    0.79641782,
                    0.79641782,
                    0.79641782,
                    0.79641782,
                    0.79199601,
                    0.78751440,
                    0.78751440,
                    0.78299851,
                    0.78299851,
                    0.78299851,
                    0.77399921,
                    0.76948916,
                    0.76948916,
                    0.76948916,
                    0.76485953,
                    0.76485953,
                    0.76485953,
                    0.76019361,
                    0.74121997,
                    0.73645978,
                    0.73169312,
                    0.73169312,
                    0.72207614,
                    0.71243405,
                    0.69782348,
                    0.69782348,
                    0.69290598,
                    0.68794639,
                    0.68298070,
                    0.67797171,
                    0.67797171,
                    0.67291844,
                    0.66785897,
                    0.66283225,
                    0.65268231,
                    0.65268231,
                    0.65268231,
                    0.65268231,
                    0.64747442,
                    0.64217326,
                    0.63136901,
                    0.62581129,
                    0.62034441,
                    0.61471946,
                    0.61471946,
                    0.60908658,
                    0.59779711,
                    0.58647606,
                    0.58057323,
                    0.57453743,
                    0.57453743,
                    0.56230639,
                    0.55603806,
                    0.55603806,
                    0.54982894,
                    0.54982894,
                    0.54374732,
                    0.53758644,
                    0.53758644,
                    0.53758644,
                    0.53111716,
                    0.53111716,
                    0.53111716,
                    0.53111716,
                    0.53111716,
                    0.53111716,
                    0.53111716,
                    0.50411552,
                    0.48370847,
                    0.48370847,
                    0.47687598,
                    0.46992880,
                    0.46286103,
                    0.46286103,
                    0.46286103,
                    0.46286103,
                    0.46286103,
                    0.46286103,
                    0.46286103,
                    0.44697851,
                    0.44697851,
                    0.44697851,
                    0.44697851,
                    0.44697851,
                    0.43839103,
                    0.42959328,
                    0.42959328,
                    0.41149538,
                    0.40237133,
                    0.40237133,
                    0.39319691,
                    0.38375661,
                    0.37425905,
                    0.37425905,
                    0.36446164,
                    0.35459805,
                    0.33466880,
                    0.32430100,
                    0.32430100,
                    0.31351936,
                    0.31351936,
                    0.30226943,
                    0.30226943,
                    0.29129049,
                    0.29129049,
                    0.29129049,
                    0.29129049,
                    0.29129049,
                    0.27785651,
                    0.27785651,
                    0.26348979,
                    0.24870485,
                    0.23349894,
                    0.23349894,
                    0.23349894,
                    0.21786076,
                    0.21786076,
                    0.20072757,
                    0.18283978,
                    0.18283978,
                    0.16174917,
                    0.10843019,
                    0.10843019,
                ],
            ]
        )
        assert_array_almost_equal(km_ci, km_var_true)

    @staticmethod
    def test_right_truncated_children(make_aids):
        event, time_enter, time_exit = make_aids("children")

        x, y, km_ci = kaplan_meier_estimator(event, time_exit.to_numpy(), time_enter.to_numpy(), conf_type="log-log")
        true_x = np.array(
            [
                7.75,
                7.50,
                7.25,
                7.0,
                6.75,
                6.50,
                6.25,
                6.0,
                5.75,
                5.50,
                5.25,
                5.00,
                4.75,
                4.50,
                4.25,
                4.0,
                3.75,
                3.5,
                3.0,
                2.75,
                2.50,
                2.25,
                1.5,
                1.0,
            ]
        )
        assert_array_almost_equal(x[::-1], true_x, 2)

        true_y = np.array(
            [
                0.0,
                0.022321,
                0.078122,
                0.156244,
                0.227263,
                0.25973,
                0.311675,
                0.370115,
                0.370115,
                0.416379,
                0.442403,
                0.474003,
                0.560185,
                0.611111,
                0.6667,
                0.6667,
                0.6667,
                0.6667,
                0.6667,
                0.6667,
                0.6667,
                1.0,
                1.0,
                1.0,
            ]
        )
        assert_array_almost_equal(y[::-1], true_y, 4)

        true_km_var = np.array(
            [
                [
                    0.0,
                    0.002822,
                    0.017862,
                    0.040298,
                    0.058612,
                    0.066127,
                    0.076545,
                    0.085444,
                    0.085444,
                    0.090003,
                    0.091389,
                    0.092039,
                    0.08619,
                    0.074768,
                    0.054073,
                    0.054073,
                    0.054073,
                    0.054073,
                    0.054073,
                    0.054073,
                    0.054073,
                    1.0,
                    1.0,
                    1.0,
                ],
                [
                    0.0,
                    0.085206,
                    0.19892,
                    0.34197,
                    0.461233,
                    0.512167,
                    0.589283,
                    0.66924,
                    0.66924,
                    0.727018,
                    0.757314,
                    0.791658,
                    0.871973,
                    0.910719,
                    0.945206,
                    0.945206,
                    0.945206,
                    0.945206,
                    0.945206,
                    0.945206,
                    0.945206,
                    1.0,
                    1.0,
                    1.0,
                ],
            ]
        )

        assert_array_almost_equal(km_ci[:, ::-1], true_km_var, 6)

    @staticmethod
    def test_right_truncated_adults(make_aids):
        event, time_enter, time_exit = make_aids("adults")

        x, y, km_ci = kaplan_meier_estimator(event, time_exit.to_numpy(), time_enter.to_numpy(), conf_type="log-log")

        true_x = np.array(
            [
                7.75,
                7.5,
                7.25,
                7,
                6.75,
                6.5,
                6.25,
                6,
                5.75,
                5.5,
                5.25,
                5,
                4.75,
                4.5,
                4.25,
                4,
                3.75,
                3.5,
                3.25,
                3,
                2.75,
                2.5,
                2.25,
                2.0,
                1.75,
                1.5,
                1.25,
                1.0,
                0.75,
                0.25,
                0.0,
            ]
        )
        assert_array_almost_equal(x[::-1], true_x, 2)

        true_y = np.array(
            [
                0,
                0.003629,
                0.004839,
                0.013825,
                0.024739,
                0.03546,
                0.055989,
                0.066794,
                0.082874,
                0.109563,
                0.131793,
                0.157752,
                0.198905,
                0.217225,
                0.244809,
                0.265913,
                0.315156,
                0.355267,
                0.384269,
                0.430015,
                0.516018,
                0.582601,
                0.683923,
                0.738636,
                0.738636,
                0.795455,
                0.875,
                1.0,
                1.0,
                1.0,
                1.0,
            ]
        )
        assert_array_almost_equal(y[::-1], true_y, 6)

        true_km_var = np.array(
            [
                [
                    0,
                    0.001217,
                    0.001784,
                    0.006516,
                    0.012517,
                    0.018518,
                    0.030121,
                    0.036221,
                    0.045398,
                    0.060644,
                    0.073313,
                    0.08817,
                    0.11157,
                    0.121917,
                    0.13762,
                    0.149678,
                    0.177624,
                    0.199998,
                    0.216144,
                    0.241542,
                    0.287968,
                    0.321501,
                    0.366806,
                    0.378508,
                    0.378508,
                    0.393153,
                    0.387,
                    1.0,
                    1.0,
                    1.0,
                    1.0,
                ],
                [
                    0,
                    0.009059,
                    0.011216,
                    0.026215,
                    0.043981,
                    0.061083,
                    0.093258,
                    0.110028,
                    0.134564,
                    0.174715,
                    0.207699,
                    0.24553,
                    0.304471,
                    0.330298,
                    0.368405,
                    0.397012,
                    0.462297,
                    0.514048,
                    0.550376,
                    0.605732,
                    0.703548,
                    0.773203,
                    0.865965,
                    0.909856,
                    0.909856,
                    0.945449,
                    0.981393,
                    1.0,
                    1.0,
                    1.0,
                    1.0,
                ],
            ]
        )

        assert_array_almost_equal(km_ci[:, ::-1], true_km_var, 6)

    @staticmethod
    def test_censoring_distribution():
        y = Surv.from_arrays(
            np.array([1, 0, 0, 1, 0, 1, 0, 1, 1, 0], dtype=bool),
            np.array([1, 2, 3, 3, 3, 4, 5, 5, 6, 7]),
        )

        probs = CensoringDistributionEstimator().fit(y).predict_proba(np.arange(1, 8))

        expected = np.array([1.0, 0.8888889, 0.6349206, 0.6349206, 0.4232804, 0.4232804, 0.0000000])

        assert_array_almost_equal(expected, probs)

    @staticmethod
    def test_reverse_conf_int(random_survival_data):
        msg = "Confidence intervals of the censoring distribution is not implemented"
        with pytest.raises(NotImplementedError, match=msg):
            kaplan_meier_estimator(
                random_survival_data["event"],
                random_survival_data["time"],
                reverse=True,
                conf_type="log-log",
            )

    @staticmethod
    def test_truncated_reverse_error(truncated_failure_data):
        with pytest.raises(
            ValueError,
            match="The censoring distribution cannot be estimated from left truncated data",
        ):
            kaplan_meier_estimator(*truncated_failure_data, reverse=True)


class TestNelsonAalen:
    @staticmethod
    @pytest.mark.parametrize("time,event,true_x,true_y", SimpleDataNACases().get_cases())
    def test_simple(time, event, true_x, true_y):
        x, y = nelson_aalen_estimator(event, time)

        assert_array_equal(x, true_x)
        assert_array_almost_equal(y, true_y)

    @staticmethod
    def test_whas500(make_whas500, whas500_true_x):
        whas500 = make_whas500(with_mean=False, with_std=False)
        time = whas500.y["lenfol"]
        event = whas500.y["fstat"]

        x, y = nelson_aalen_estimator(event, time)

        assert_array_equal(x.astype(int), whas500_true_x)

        true_y = np.array(
            [
                0.016,
                0.032260162601626,
                0.038458509709064,
                0.0426165138670682,
                0.0467918792115359,
                0.0572740595050369,
                0.0699859239118166,
                0.0764236921521598,
                0.0850630010074515,
                0.089420299482397,
                0.0916084832898368,
                0.0959944482021175,
                0.102602377717536,
                0.109254262418201,
                0.113718548132487,
                0.118202853065222,
                0.120455105317475,
                0.12271244166059,
                0.127237328538418,
                0.1340555103566,
                0.13634383987605,
                0.138637417857702,
                0.140936268432414,
                0.143240415897852,
                0.145549884720023,
                0.147864699534838,
                0.150184885149687,
                0.152510466545036,
                0.157172471207041,
                0.159514391581748,
                0.161861809422123,
                0.164214750598594,
                0.16893173173067,
                0.173671068223561,
                0.176052020604513,
                0.178438655449382,
                0.180830999946989,
                0.183229081481762,
                0.185632927635608,
                0.188042566189824,
                0.190458025127023,
                0.192879332633076,
                0.195306517099095,
                0.197739607123426,
                0.20017863151367,
                0.202623619288731,
                0.205074599680888,
                0.20753160213789,
                0.209994656325083,
                0.212463792127552,
                0.214939039652304,
                0.217420429230468,
                0.219907991419523,
                0.222401757005558,
                0.224901757005558,
                0.229914288333878,
                0.232433180021536,
                0.234958432546788,
                0.237490078116409,
                0.240028149182399,
                0.242572678444485,
                0.247674719260812,
                0.252802924389017,
                0.255380243976646,
                0.257964223304811,
                0.26055489687994,
                0.263152299477343,
                0.26575646614401,
                0.268367432201451,
                0.273603034295692,
                0.27623461324306,
                0.278873135670501,
                0.281518638316003,
                0.284171158209903,
                0.286830732677988,
                0.292164066011321,
                0.294845031158774,
                0.297533203201785,
                0.300228620991542,
                0.302931323694245,
                0.305641350794516,
                0.308358742098864,
                0.311083537739191,
                0.313815778176349,
                0.319295230231144,
                0.322050051167783,
                0.322050051167783,
                0.322050051167783,
                0.322050051167783,
                0.322050051167783,
                0.324866952576234,
                0.327691811333296,
                0.327691811333296,
                0.327691811333296,
                0.330548954190439,
                0.333414283703333,
                0.333414283703333,
                0.333414283703333,
                0.333414283703333,
                0.333414283703333,
                0.33633826031152,
                0.339270811631168,
                0.339270811631168,
                0.339270811631168,
                0.339270811631168,
                0.339270811631168,
                0.339270811631168,
                0.339270811631168,
                0.342273814634171,
                0.342273814634171,
                0.345294962670425,
                0.345294962670425,
                0.345294962670425,
                0.345294962670425,
                0.345294962670425,
                0.345294962670425,
                0.345294962670425,
                0.348390937902623,
                0.348390937902623,
                0.351545512035115,
                0.351545512035115,
                0.351545512035115,
                0.351545512035115,
                0.351545512035115,
                0.351545512035115,
                0.351545512035115,
                0.351545512035115,
                0.354824200559705,
                0.354824200559705,
                0.358124530592708,
                0.361435788870854,
                0.361435788870854,
                0.361435788870854,
                0.364780270476205,
                0.364780270476205,
                0.364780270476205,
                0.368158648854584,
                0.368158648854584,
                0.368158648854584,
                0.368158648854584,
                0.368158648854584,
                0.368158648854584,
                0.368158648854584,
                0.368158648854584,
                0.368158648854584,
                0.368158648854584,
                0.368158648854584,
                0.368158648854584,
                0.371717367715794,
                0.371717367715794,
                0.375314490017952,
                0.378937678423749,
                0.382574042060113,
                0.382574042060113,
                0.382574042060113,
                0.382574042060113,
                0.386305385343695,
                0.386305385343695,
                0.390064783839935,
                0.393838368745596,
                0.393838368745596,
                0.393838368745596,
                0.393838368745596,
                0.393838368745596,
                0.393838368745596,
                0.393838368745596,
                0.393838368745596,
                0.393838368745596,
                0.397775376619611,
                0.401727945789572,
                0.401727945789572,
                0.401727945789572,
                0.405727945789572,
                0.4097440100466,
                0.413776268111116,
                0.417824851107068,
                0.421889891757474,
                0.421889891757474,
                0.421889891757474,
                0.426005118094923,
                0.430137349499881,
                0.430137349499881,
                0.434304016166548,
                0.438488116584958,
                0.442689797257227,
                0.442689797257227,
                0.44692708539282,
                0.451182404541756,
                0.45545590881526,
                0.459747754308823,
                0.464058099136409,
                0.468387103465413,
                0.47273492955237,
                0.477101741779444,
                0.481487706691725,
                0.485892993035337,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.490317771796399,
                0.495125464104092,
                0.495125464104092,
                0.495125464104092,
                0.495125464104092,
                0.500075959153596,
                0.500075959153596,
                0.505075959153596,
                0.505075959153596,
                0.505075959153596,
                0.505075959153596,
                0.505075959153596,
                0.510204164281801,
                0.510204164281801,
                0.510204164281801,
                0.515412497615135,
                0.515412497615135,
                0.515412497615135,
                0.515412497615135,
                0.515412497615135,
                0.515412497615135,
                0.515412497615135,
                0.515412497615135,
                0.515412497615135,
                0.520937359493588,
                0.520937359493588,
                0.520937359493588,
                0.520937359493588,
                0.526587077007712,
                0.526587077007712,
                0.526587077007712,
                0.526587077007712,
                0.532367423828521,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.538249776769698,
                0.544872293325989,
                0.544872293325989,
                0.544872293325989,
                0.544872293325989,
                0.544872293325989,
                0.544872293325989,
                0.544872293325989,
                0.544872293325989,
                0.551914546847116,
                0.551914546847116,
                0.551914546847116,
                0.551914546847116,
                0.551914546847116,
                0.551914546847116,
                0.551914546847116,
                0.551914546847116,
                0.551914546847116,
                0.551914546847116,
                0.551914546847116,
                0.551914546847116,
                0.559788562595148,
                0.559788562595148,
                0.559788562595148,
                0.559788562595148,
                0.559788562595148,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.576182005218098,
                0.586182005218098,
                0.596283015319109,
                0.606487096951762,
                0.616796375302277,
                0.627213041968944,
                0.637739357758417,
                0.648377655630758,
                0.659130343802801,
                0.669999909020192,
                0.680988920009203,
                0.692100031120314,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.703335986176494,
                0.718261359310822,
                0.718261359310822,
                0.718261359310822,
                0.718261359310822,
                0.718261359310822,
                0.718261359310822,
                0.718261359310822,
                0.718261359310822,
                0.718261359310822,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.735805218959945,
                0.819138552293278,
                0.819138552293278,
                0.819138552293278,
                0.819138552293278,
                0.819138552293278,
                0.819138552293278,
                0.819138552293278,
                0.819138552293278,
                0.819138552293278,
                1.15247188562661,
                1.65247188562661,
                2.65247188562661,
            ]
        )

        assert_array_almost_equal(y, true_y)


class SimpleDataBMTCases(FixtureParameterFactory):
    def _load(self, dis=None):
        dis_df, bmt = load_bmt()
        event = bmt["status"]
        time = bmt["ftime"]
        if dis is not None:
            dis_np = dis_df["dis"].to_numpy()
            dis_filter = dis_np == dis
            event = event[dis_filter]
            time = time[dis_filter]
        return event, time

    def data_bmt_full(self):
        event, time = self._load()

        true_x = np.array([0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 13, 14, 22, 26, 32, 35, 67, 68, 70, 72])
        true_y = np.array(
            [
                [0.02857143, 0.02857143],
                [0.05714286, 0.05714286],
                [0.05714286, 0.08571429],
                [0.14581281, 0.11527094],
                [0.20492611, 0.14482759],
                [0.20492611, 0.1757917],
                [0.20492611, 0.23771992],
                [0.23771164, 0.27050544],
                [0.23771164, 0.30563279],
                [0.23771164, 0.34076015],
                [0.27283899, 0.34076015],
                [0.27283899, 0.3758875],
                [0.27283899, 0.41101485],
                [0.27283899, 0.4461422],
                [0.27283899, 0.48126955],
                [0.27283899, 0.48126955],
                [0.27283899, 0.48126955],
                [0.27283899, 0.48126955],
                [0.27283899, 0.48126955],
                [0.27283899, 0.48126955],
                [0.27283899, 0.48126955],
            ]
        ).T

        return event, time, true_x, true_y

    def data_bmt_total_risk(self):
        event, time = self._load(dis="0")

        true_x = np.array([0, 1, 3, 4, 5, 7, 8, 9, 12, 13, 14, 22, 26, 35, 72])
        true_y = np.array(
            [
                [0.05882353, 0.05882353],
                [0.11764706, 0.11764706],
                [0.11764706, 0.17647059],
                [0.11764706, 0.23529412],
                [0.11764706, 0.29411765],
                [0.11764706, 0.35294118],
                [0.11764706, 0.41176471],
                [0.11764706, 0.47058824],
                [0.17647059, 0.47058824],
                [0.17647059, 0.52941176],
                [0.17647059, 0.58823529],
                [0.17647059, 0.64705882],
                [0.17647059, 0.70588235],
                [0.17647059, 0.70588235],
                [0.17647059, 0.70588235],
            ]
        ).T

        return event, time, true_x, true_y

    def data_bmt_AML(self):
        event, time = self._load(dis="1")

        true_x = np.array([2, 3, 4, 7, 8, 10, 32, 35, 67, 68, 70])
        true_y = np.array(
            [
                [0.0, 0.05555556],
                [0.17708333, 0.05555556],
                [0.29513889, 0.05555556],
                [0.29513889, 0.12048611],
                [0.36818576, 0.12048611],
                [0.36818576, 0.20570747],
                [0.36818576, 0.20570747],
                [0.36818576, 0.20570747],
                [0.36818576, 0.20570747],
                [0.36818576, 0.20570747],
                [0.36818576, 0.20570747],
            ]
        ).T

        return event, time, true_x, true_y

    @staticmethod
    def data_three_competing_cases():
        time = np.array(
            [
                5.3,
                10.2,
                4.5,
                12.7,
                7.4,
                8.6,
                9.1,
                3.3,
                6.9,
                11.5,
                15.2,
                13.1,
                10.0,
                5.9,
                8.2,
                4.3,
                6.1,
                14.7,
                16.9,
                3.7,
                5.0,
                7.8,
                9.5,
                11.9,
                10.6,
                6.4,
                4.8,
                12.3,
                14.0,
            ]
        )

        event = np.array([1, 0, 3, 0, 2, 3, 0, 1, 3, 0, 1, 0, 3, 0, 2, 0, 0, 3, 0, 1, 0, 3, 1, 0, 2, 0, 3, 2, 0])
        true_x = np.array(
            [
                3.3,
                3.7,
                4.3,
                4.5,
                4.8,
                5.0,
                5.3,
                5.9,
                6.1,
                6.4,
                6.9,
                7.4,
                7.8,
                8.2,
                8.6,
                9.1,
                9.5,
                10.0,
                10.2,
                10.6,
                11.5,
                11.9,
                12.3,
                12.7,
                13.1,
                14.0,
                14.7,
                15.2,
                16.9,
            ]
        )
        true_y = np.array(
            [
                [
                    0.03448276,
                    0.06896552,
                    0.06896552,
                    0.06896552,
                    0.06896552,
                    0.06896552,
                    0.10633145,
                    0.10633145,
                    0.10633145,
                    0.10633145,
                    0.10633145,
                    0.10633145,
                    0.10633145,
                    0.10633145,
                    0.10633145,
                    0.10633145,
                    0.15292541,
                    0.15292541,
                    0.15292541,
                    0.15292541,
                    0.15292541,
                    0.15292541,
                    0.15292541,
                    0.15292541,
                    0.15292541,
                    0.15292541,
                    0.15292541,
                    0.28471974,
                    0.28471974,
                ],
                [
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.04326582,
                    0.04326582,
                    0.08653163,
                    0.08653163,
                    0.08653163,
                    0.08653163,
                    0.08653163,
                    0.08653163,
                    0.13778498,
                    0.13778498,
                    0.13778498,
                    0.20368215,
                    0.20368215,
                    0.20368215,
                    0.20368215,
                    0.20368215,
                    0.20368215,
                    0.20368215,
                ],
                [
                    0.0,
                    0.0,
                    0.0,
                    0.03580902,
                    0.07161804,
                    0.07161804,
                    0.07161804,
                    0.07161804,
                    0.07161804,
                    0.07161804,
                    0.11488385,
                    0.11488385,
                    0.15814967,
                    0.15814967,
                    0.20141549,
                    0.20141549,
                    0.20141549,
                    0.24800944,
                    0.24800944,
                    0.24800944,
                    0.24800944,
                    0.24800944,
                    0.24800944,
                    0.24800944,
                    0.24800944,
                    0.24800944,
                    0.37980378,
                    0.37980378,
                    0.37980378,
                ],
            ]
        )

        return event, time, true_x, true_y


class CGVHD_DataSets(FixtureParameterFactory):
    """The true CI values for Aalen and Delta_Approx were generated using
    the proc lifetest statement in SAS.
    https://support.sas.com/documentation/onlinedoc/stat/ex_code/143/liftcrsk.html

    Because an external implementation of the exact Dinse variance has not been found,
    the true CI values are taken from this internal code. Thus, this serves as a regression test.
    """

    @staticmethod
    def data_cgvhd_factory():
        def _load_cgvhd(var_type=None):
            _, df = load_cgvhd()
            event, ftime = df["status"], df["ftime"]

            if var_type == "Aalen":
                expected_file = CGVHD_AALEN_FILE
            elif var_type == "Dinse_Approx":
                expected_file = CGVHD_DELTA_FILE
            elif var_type == "Dinse":
                expected_file = CGVHD_DINSE_FILE
            else:
                raise AssertionError(f"{var_type} not supported")

            data = pd.DataFrame(
                np.load(expected_file), columns=["Failcode", "dftime", "CIF", "CIF_LCL", "CIF_UCL"]
            ).query("dftime > 0")
            true_x = data.loc[data.Failcode == 1, "dftime"].to_numpy()

            _, km_y, km_ci = kaplan_meier_estimator(event > 0, ftime, conf_type="log-log")

            true_y = [1.0 - km_y]
            true_ci = [1.0 - km_ci]
            for _, subet in data.groupby("Failcode"):
                true_y.append(subet["CIF"].to_numpy())
                true_ci.append(np.stack((subet["CIF_LCL"].to_numpy(), subet["CIF_UCL"].to_numpy())))

            true_y = np.stack(true_y)
            true_ci = np.stack(true_ci)

            if var_type is None:
                return event, ftime, true_x, true_y
            return event, ftime, true_x, true_y, true_ci

        return (_load_cgvhd,)


class TestCumIncCompetingRisks:
    @staticmethod
    @pytest.mark.parametrize("event, time, true_x, true_y", SimpleDataBMTCases().get_cases())
    def test_wrong_dtype(event, time, true_x, true_y):
        event = event.astype(bool)
        with pytest.raises(ValueError, match=f"Elements of event indicator must be integer, but found {event.dtype}"):
            _x, _y = cumulative_incidence_competing_risks(event, time)

    @staticmethod
    @pytest.mark.parametrize("event, time, true_x, true_y", SimpleDataBMTCases().get_cases())
    def test_non_negative(event, time, true_x, true_y):
        event = event.copy()
        event[0] = -1
        with pytest.raises(ValueError, match="Elements of event indicator must be non-negative"):
            _x, _y = cumulative_incidence_competing_risks(event, time)

    @staticmethod
    @pytest.mark.parametrize("event, time, true_x, true_y", SimpleDataBMTCases().get_cases())
    def test_all_risks_appear(event, time, true_x, true_y):
        mask = event != 1
        event = np.compress(mask, event)
        time = np.compress(mask, time)
        with pytest.raises(ValueError, match="Some risks do not appear in the event array."):
            _x, _y = cumulative_incidence_competing_risks(event, time)

    @staticmethod
    @pytest.mark.parametrize("event, time, true_x, true_y", SimpleDataBMTCases().get_cases())
    def test_full(event, time, true_x, true_y):
        x, y = cumulative_incidence_competing_risks(event, time)

        assert_array_equal(x, true_x)
        _x_km, true_km = kaplan_meier_estimator(event > 0, time)
        assert_array_almost_equal(y[0], 1.0 - true_km)
        assert_array_almost_equal(y[1:], true_y)
        assert_array_almost_equal(y[0], y[1:].sum(axis=0))

    @staticmethod
    @pytest.mark.parametrize("event, time, true_x, true_y", SimpleDataBMTCases().get_cases())
    def test_truncated(event, time, true_x, true_y):
        time_min = -1
        x, y = cumulative_incidence_competing_risks(event, time, time_min=time_min)

        assert_array_equal(x, true_x)
        _x_km, true_km = kaplan_meier_estimator(event > 0, time, time_min=time_min)
        assert_array_almost_equal(y[0], 1.0 - true_km)
        assert_array_almost_equal(y[1:], true_y)
        assert_array_almost_equal(y[0], y[1:].sum(axis=0))

    @staticmethod
    @pytest.mark.parametrize("var_type", ["Aalen", "Dinse_Approx", "Dinse"])
    @pytest.mark.parametrize("get_expected_fn", CGVHD_DataSets().get_cases())
    def test_ci(get_expected_fn, var_type):
        event, time, true_x, true_y, true_ci = get_expected_fn(var_type=var_type)

        x, y, ci = cumulative_incidence_competing_risks(event, time, conf_type="log-log", var_type=var_type)

        assert_array_equal(x, true_x)
        assert_array_almost_equal(y, true_y)
        assert_array_almost_equal(y[0], y[1:].sum(axis=0))

        nan_mask = np.isnan(true_ci)
        assert_allclose(np.ma.masked_array(ci, nan_mask), np.ma.masked_array(true_ci, nan_mask), rtol=5.0e-4)
        assert_array_equal(np.ma.masked_array(ci, ~nan_mask), np.ma.masked_array(np.zeros_like(ci), ~nan_mask))

    @staticmethod
    @pytest.mark.parametrize("event, time, true_x, true_y", SimpleDataBMTCases().get_cases())
    @pytest.mark.parametrize("conf_level", [None, -1, 1.0, 3.0, np.inf, np.nan])
    def test_invalid_conf_level_competing_risks(event, time, true_x, true_y, conf_level):
        msg = f"conf_level must be a float in the range (0.0, 1.0), but was {conf_level}"
        with pytest.raises(ValueError, match=re.escape(msg)):
            cumulative_incidence_competing_risks(event, time, conf_level=conf_level, conf_type="log-log")

    @staticmethod
    @pytest.mark.parametrize("event, time, true_x, true_y", SimpleDataBMTCases().get_cases())
    @pytest.mark.parametrize("conf_type", ["None", -1, "", "not"])
    def test_invalid_conf_type_competing_risks(event, time, true_x, true_y, conf_type):
        msg = f"conf_type must be None or a str among {{'log-log'}}, but was {conf_type!r}"
        with pytest.raises(ValueError, match=msg):
            cumulative_incidence_competing_risks(event, time, conf_level=0.9, conf_type=conf_type)

    @staticmethod
    @pytest.mark.parametrize("event, time, true_x, true_y", SimpleDataBMTCases().get_cases())
    @pytest.mark.parametrize("var_type", ["None", "dinse", 1, "", "not"])
    def test_invalid_var_type_competing_risks(event, time, true_x, true_y, var_type):
        msg = f"{var_type=} must be one of 'Aalen', 'Dinse', or 'Dinse_Approx'."
        with pytest.raises(ValueError, match=msg):
            cumulative_incidence_competing_risks(event, time, conf_level=0.95, conf_type="log-log", var_type=var_type)
